CH3 - Simple Sorting
CH3 - Simple Sorting
STRUCTURES
By L. Mutanu
SORTING
Because sorting is so important and potentially so timeconsuming, it has been the subject of extensive research in
computer science, and as a result various sort algorithms have
been developed that range from very simple to very complex.
By L. Mutanu
BUBBLE SORT
View Animatio
n
You continue until you reach the end. Your array may not be fully sorted but the
largest number has been pushed to the end. This must be true because, as soon
as you encounter the largest, youll end up swapping it every time you compare
two numbers, until eventually it reaches the end of the array.
Then repeat the process again. This time because the largest is at the end you
only need to go through N-1 elements and the next time N-2 and so on until you
are left with one element. This shows that we will repeat the process N-1 times.
By L. Mutanu
The simplified algorithm would be: The actual bubble algorithm should appear as follows:
void bubbleSort()
Go through the array swapping
{
Repeat this process N-1 times
int out, in;
for(out=10-1; out>1; out--) // repeats process N-1 times
for(in=0; in<out; in++) //Go though array
if( v[in] > v[in+1] ) //out of order?
{
double temp = v[in]; //swap them
v[in] = v[in+1];
v[in+1] = temp;
]
}
By L. Mutanu
void bubbleSort()
{
int out, in;
for(out=10-1; out>1; out--) // repeats
process N-1 times
for(in=0; in<out; in++) //Go though array
if( v[in] > v[in+1] ) //out of order?
{
double temp = v[in]; //swap them
v[in] = v[in+1];
v[in+1] = temp;
}
}
}; //end class ArrayBub
int main()
{
ArrayBub arr; //create the array
arr.insert(); //insert 10 items
arr.display(); //display items
arr.bubbleSort(); //bubble sort them
arr.display(); //display them again
return 0;
} //end main()
By L. Mutanu
Exercise:
Can you modify the program to sort the names of employees
The BubbleSort algorithm makes N-1 comparisons on the first pass, N2 on the second, and so on, down to one comparison on the last pass.
Thus in total the number of comparisons will be (N-1) + (N-2) + (N-3)
+ ... + 1 = N*(N-1)/2
77
99
44
55
22
99
77
99
44
55
22
99
77
99
44
55
22
77
99
99
55
22
77
99
99
55
22
77
77
99
55
22
44
77
99
55
22
44
INSERTION SORT
This algorithm starts out like the bubble sort. It compares the
two top elements and the smallest is kept at the top.
It then compares the second and third element and smallest is
kept at position two. Position two is then compared with position
1 and swapping takes place if the smallest is in position 1.
It still executes in O(N2) time, but its about twice as fast as the
bubble sort if the data is almost sorted.
HINT: Whenever you see nested loops such as those in the
bubble sort, you can suspect that an algorithm runs in O(N 2)
time. The outer loop executes N times, and the inner loop
executes N (or perhaps N divided by some constant) times for
each cycle of the outer loop. This means youre doing
something approximately N*N or N2 times.
View
animation
void insertionSort()
{
int in, out;
for(out=1; out<10; out++) //loops n-1 times (1 to 9)
{
double temp = v[out];
//store right element in temp
in = out;
//start shifts at temp
while(in>0 && v[in-1] >= temp) //do to last left and if out of
order,
{
v[in] = v[in-1];
//swap
- -in;
//go left one position
}
v[in] = temp;
//insert marked item when not out
// of order and before last left. this is the
correct
//position
}
}
By L. Mutanu
While the time taken is the same as for the bubble sort (it
makes the same number of comparisons) it takes a shorter
time to swap if the data is almost sorted.
By L. Mutanu