bubble-sort

Assembly bubble sort swap

若如初见. 提交于 2019-11-27 16:31:19
I'm trying to do a bubble sort in x86 assembly (yes it has to be bubble, as I'm not concerned about speed optimization regarding different types of sorts) and for some reason, my code will not swap the necessary values. Here is my code mov eax, list ;store list in eax mov edx,[eax+4*edi-4] ;temp = var1 cmp edx,[eax+edi*4] ;compare JLE SECOND_LOOP ;jump if var1 < var2 mov [eax+4*edi-4],[eax+edi*4] ;var1 = var2 mov [eax+edi*4], edx ;var2 = temp jmp SECOND_LOOP At the last mov instruction where it's supposed to load the temp back into the address, it..doesn't. The EAX register has the starting

Assembly - bubble sort for sorting string

馋奶兔 提交于 2019-11-27 16:10:43
I am writing a program in assembly using tasm. My task is to write a program that will use bubble sort to sort entered string alphabetically. Ex. if you enter "hello" it should write "ehllo". I have writened the begging to enter string and to sort it (I think it works okey until the end where it should print out the result, but at the end it just writes my .data once and the finisheds its work) P.S sorry for bad english .model small .stack 100h .data request db 'This program is using bubblesort to get alphabetical order of your enterd string', 0Dh, 0Ah, 'Enter your string:', 0Dh, 0Ah, '$'

How to sort a linked list using bubble-sort?

萝らか妹 提交于 2019-11-27 12:57:44
I am trying to use bubble-sort in order to sort a linked list. I use curr and trail in order to traverse thru the list. curr is supposed to be one step ahead of trail always. This is my code so far: void linked_list::sort () { int i,j=0; int counter=0; node *curr=head; node *trail=head; node *temp=NULL; while (curr !=NULL) { curr=curr->next; //couting the number of items I have in my list. counter++; //this works fine. } curr=head->next; // reseting the curr value for the 2nd position. for (i=0; i<counter; i++) { while (curr != NULL) { if (trail->data > curr->data) { temp=curr->next; //bubble

Bubble sort algorithm JavaScript [closed]

偶尔善良 提交于 2019-11-27 12:57:12
Please can you tell me what is wrong to this implementation of bubble sort algorithm in JavaScript? for (var i=1; i<records.length; i++){ for (var j=records.length; j<1; j--){ if (parseInt(records[i-1]) < parseInt(records[i])){ var temp = records[i-1]; records[i-1] = records[i] records[i] = temp; } } } Ignatius Andrew Couple of codes for bubble sort bubblesort should not be used for larger arrays, can be used for smaller ones for its simplicity. Method 1 var a = [33, 103, 3, 726, 200, 984, 198, 764, 9]; function bubbleSort(a) { var swapped; do { swapped = false; for (var i=0; i < a.length-1; i

What's the most elegant way to bubble-sort in C#?

假装没事ソ 提交于 2019-11-27 05:50:51
问题 Can this be cleaned up? using System; class AscendingBubbleSort { public static void Main() { int i = 0,j = 0,t = 0; int []c=new int[20]; for(i=0;i<20;i++) { Console.WriteLine("Enter Value p[{0}]:", i); c[i]=int.Parse(Console.ReadLine()); } // Sorting: Bubble Sort for(i=0;i<20;i++) { for(j=i+1;j<20;j++) { if(c[i]>c[j]) { Console.WriteLine("c[{0}]={1}, c[{2}]={3}", i, c[i], j, c[j]); t=c[i]; c[i]=c[j]; c[j]=t; } } } Console.WriteLine("bubble sorted array:"); // sorted array output for(i=0;i<20

Assembly bubble sort swap

半世苍凉 提交于 2019-11-27 04:08:42
问题 I'm trying to do a bubble sort in x86 assembly (yes it has to be bubble, as I'm not concerned about speed optimization regarding different types of sorts) and for some reason, my code will not swap the necessary values. Here is my code mov eax, list ;store list in eax mov edx,[eax+4*edi-4] ;temp = var1 cmp edx,[eax+edi*4] ;compare JLE SECOND_LOOP ;jump if var1 < var2 mov [eax+4*edi-4],[eax+edi*4] ;var1 = var2 mov [eax+edi*4], edx ;var2 = temp jmp SECOND_LOOP At the last mov instruction where

What is a bubble sort good for? [closed]

牧云@^-^@ 提交于 2019-11-27 03:15:26
Do bubble sorts have any real world use? Every time I see one mentioned, it's always either: A sorting algorithm to learn with. An example of a sorting algorithm not to use. Remy Sharp It depends on the way your data is distributed - if you can make some assumptions. One of the best links I've found to understand when to use a bubble sort - or some other sort, is this - an animated view on sorting algorithms: http://www.sorting-algorithms.com/ Bubble sort is (provably) the fastest sort available under a very specific circumstance. It originally became well known primarily because it was one of

Why is bubble sort O(n^2)?

放肆的年华 提交于 2019-11-27 02:39:21
问题 int currentMinIndex = 0; for (int front = 0; front < intArray.length; front++) { currentMinIndex = front; for (int i = front; i < intArray.length; i++) { if (intArray[i] < intArray[currentMinIndex]) { currentMinIndex = i; } } int tmp = intArray[front]; intArray[front] = intArray[currentMinIndex]; intArray[currentMinIndex] = tmp; } The inner loop is iterating: n + (n-1) + (n-2) + (n-3) + ... + 1 times. The outer loop is iterating: n times. So you get n * (the sum of the numbers 1 to n) Isn't

Optimized Bubble Sort (Java)

廉价感情. 提交于 2019-11-26 22:58:31
问题 I would like to know how else I can optimize bubble sort so that it overlooks elements that have already been sorted, even after the first pass. Eg. [4, 2, 3, 1, 5, 6] --> [2, 3, 1, **4, 5, 6**] We observe that [4,5,6] are already in sorted order, how can modify my code so that it overlooks this 3 elements in the next pass? (which means the sort would be more efficient?) Do you suggest a recursive method? public static void bubblesort(int[] a) { for(int i=1; i<a.length; i++) { boolean is

c++ sort with structs

两盒软妹~` 提交于 2019-11-26 19:06:29
I am having a hard time with this problem which requires a sort of customer names, customer ids, and finally amount due. I have the whole program figured, but cannot figure out the last prototype needed to do the sorting. i have a struct called Customers, and i will provide the int main() part also. I just need any help to gt started on the prototype SortData(). struct Customers { string Name; string Id; float OrderAmount; float Tax; float AmountDue; }; const int MAX_CUSTOMERS = 1000; bool MoreCustomers(int); Customers GetCustomerData(); void OutputResults(Customers [], int); void SortData