Thursday, July 30, 2009

How to sort a One-Dimensional Array in Turbo C?

5 Numbers or more

How to sort a One-Dimensional Array in Turbo C?
Bubble Sort








The bubble sort gets its name because as elements are sorted they gradually "bubble" (or rise) to their proper positions, like bubbles rising in a glass of soda. The bubble sort repeatedly compares adjacent elements of an array, starting with the first and second elements, and swapping them if they are out of order. After the first and second elements are compared, the second and third elements are compared, and swapped if they are out of order. This process continues until the end of the list is reached.

















When the end is reached, the bubble sort returns to elements one and two and starts the process all over again. So, when does it stop? The bubble sort knows that it is finished when it examines the entire array and no "swaps" are needed (thus the list is in proper order). The bubble sort keeps track of the occurrence of swaps by the use of a flag.





The table below follows an array of numbers before, during, and after a bubble sort for descending order. A "pass" is defined as one full trip through the array comparing and if necessary, swapping, adjacent elements. Several passes have to be made through the array before it is finally sorted.











Array at beginning: 84 69 76 86 94 91


After Pass #1: 84 76 86 94 91 69


After Pass #2: 84 86 94 91 76 69


After Pass #3: 86 94 91 84 76 69


After Pass #4: 94 91 86 84 76 69


After Pass #5 (done): 94 91 86 84 76 69





The bubble sort is an easy algorithm to program, but it is slower than many other sorts. With a bubble sort, it is always necessary to make one final "pass" through the array to check to see that no swaps are made to ensure that the process is finished. In actuality, the process is finished before this last pass is made.





// Bubble Sort Function for Descending Order


void main()


{


int i, j, flag = 1; // set flag to 1 to begin initial pass


int temp; // holding variable


int arrayLength = array.length( );


for(i = 1; (i %26lt;= arrayLength) %26amp;%26amp; flag; i++)


{


flag = 0;


for (j=0; j %26lt; (arrayLength -1); j++)


{


if (array[j+1] %26gt; array[j]) // ascending order simply changes to %26lt;


{


temp = array[j]; // swap elements


array[j] = array[j+1];


array[j+1] = temp;


flag = 1; // indicates that a swap occurred.


}


}


}


}
Reply:Well, assuming a size parameter (and the array being passed simulated call by reference with ints):





//Begin function BubbleSort


void BubbleSort(int arr[], int size)


{


int i, j, temp;


for(j=0; j%26lt;size; j++)


for(i=0; i%26lt;size-1; i++)


if(arr[i]%26gt;arr[i+1]){


temp=arr[i];


arr[i]=arr[i+1];


arr[i+1]=temp;}


}


//End function BubbleSort





This isn't the fastest sort, but it's easy and common.





The quickest way to find the size of an array:





sizeof(arr)/sizeof(*arr);





Note that *arr dereferences the constant pointer to the first element of the array.
Reply:*barfs*





Two people suggesting the most inefficient sorting algorithm in existence. Read this article to learn how to really sort data.





http://eternallyconfuzzled.com/tuts/algo...





In reality, for the size of the array that you will be sorting, the time a bubble sort will take vs a more efficient sort is negligible and you should still know about it. Regardless, you should also know you other (better) options. I won't get down too much on the answers above me because they both did acknowledge that it is an inefficient sort, however... still read his article, it is good.


No comments:

Post a Comment