Professional Documents
Culture Documents
Sorting
• Sorting is a process that organizes a collection of data into either ascending or
descending order.
• An internal sort requires that the collection of data fit entirely in the
computer’s main memory.
• We can use an external sort when the collection of data cannot fit in the
computer’s main memory all at once but must reside in secondary storage such
as on a disk.
8 23 78 45 32 56 After pass 1
8 23 32 78 45 56
After pass 2
8 23 32 45 78 56
After pass 3
8 23 32 45 56 78
After pass 4
Pass 1
23 78 45 8 32 56
23 78 45 8 32 56
23 78 8 45 32 56
23 8 78 45 32 56
8 23 78 45 32 56
8 23 78 45 32 56
Pass 2
8 23 78 45 32 56
8 23 78 32 45 56
8 23 32 78 45 56
8 23 32 78 45 56
8 23 32 78 45 56
Pass 3
8 23 32 78 45 56
8 23 32 45 78 56
8 23 32 45 78 56
8 23 32 45 78 56
Pass 4
8 23 32 45 78 56
8 23 32 45 56 78
8 23 32 45 56 78
8 23 32 45 56 78
Pass 5
8 23 32 45 56 78
8 23 32 45 56 78
Bubble Sort Algorithm
void bubleSort(int a[], int n)
{
for (int i = 0; i < n-1; i++){
for (int j = n-1; j > i; j--)
if (a[j-1] > a[j]){
swap(a[j],a[j-1]);
}
}
}
Function to check array is Sorted
void bubleSort(int a[], int n)
{
bool sorted = true;
for (int j=n-1; j > 0; j--)
if (a[j-1] > a[j]){
sorted = false;
}
if (sorted)
cout<<“Array is sorted\n”;
else
cout<<“Array is not sorted\n”;
}
Finally:- Bubble Sort Algorithm
void bubleSort(int a[], int n)
{
bool sorted = false;
int last = n-1;
• Worst-case: O(n2)
– Array is in reverse order:
– Outer loop is executed n-1 times,
– The number of moves: 3*(1+2+...+n-1) = 3 * n*(n-1)/2 O(n2)
– The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2 O(n2)
• Average-case: O(n2)
– We have to look at all possible initial data organizations.
23 78 45 8 32 56 Original List
8 78 45 23 32 56 After pass 1
8 23 45 78 32 56
After pass 2
8 23 32 78 45 56
After pass 3
8 23 32 45 78 56
After pass 4
8 23 32 45 56 78
After pass 5
Selection Sort (cont.)
void selectionSort( int a[], int n) {
for (int i = 0; i < n-1; i++) {
int min = i;
for (int j = i+1; j < n; j++)
if (a[j] < a[min]) min = j;
swap(a[i], a[min]);
}
}
23 78 45 8 32 56 Original List
23 78 45 8 32 56 After pass 1
23 45 78 8 32 56
After pass 2
8 23 45 78 32 56
After pass 3
8 23 32 45 78 56
After pass 4
8 23 32 45 56 78
After pass 5
Pass 4
8 23 45 78 32 56
Temp 32
8 23 45 78 78 56
8 23 45 45 32 56
8 23 32 45 32 56
8 23 32 45 32 56
Insertion Sort Algorithm
void insertionSort(int a[], int n)
{
for (int i = 1; i < n; i++)
{
int tmp = a[i];
a[j] = tmp;
}
}
Insertion Sort – Analysis
• Running time depends on not only the size of the array but also
the contents of the array.
• Best-case: O(n)
– Array is already sorted in ascending order.
– Inner loop will not be executed.
– The number of moves: 2*(n-1) O(n)
– The number of key comparisons: (n-1) O(n)
• Worst-case: O(n2)
– Array is in reverse order:
– Inner loop is executed i-1 times, for i = 2,3, …, n
– The number of moves: 2*(n-1)+(1+2+...+n-1)= 2*(n-1)+ n*(n-1)/2 O(n2)
– The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2 O(n2)
• Average-case: O(n2)
– We have to look at all possible initial data organizations.
• So, Insertion Sort is O(n2)
Analysis of insertion sort
• Which running time will be used to characterize this
algorithm?
– Best, worst or average?
• Worst:
– Longest running time (this is the upper limit for the algorithm)
– It is guaranteed that the algorithm will not be worse than this.
• Sometimes we are interested in average case. But there are
some problems with the average case.
– It is difficult to figure out the average case. i.e. what is average
input?
– Are we going to assume all possible inputs are equally likely?
– In fact for most algorithms average case is same as the worst case.
Shellsort
• Reduce no items movements in insertion sort.
• Invented by Donald Shell in 1959.
• Also know diminishing-incrementing-sort.
• Elements of list viewed as sub-lists at a
particular distance.
• Each sub-list is sorted using insertion sort.
• Uses a sequence h1, h2, …, ht called the
increment sequence to create sub lists.
Shellsort
• Shellsort improves on the efficiency of
insertion sort by quickly shifting values to
their destination.
Shellsort
• The distance between comparisons
decreases as the sorting algorithm runs until
the last phase in which adjacent elements
are compared
Shellsort Examples
Sort: 18 32 12 5 38 33 16 2
8 Numbers to be sorted, Shell’s increment will be floor(n/2)
* floor(8/2) floor(4) = 4
18 32 12 5 38 33 16 2
Sort: 18 32 12 5 38 33 16 2
8 Numbers to be sorted, Shell’s increment will be floor(n/2)
* floor(8/2) floor(4) = 4
18 32 12 5 38 33 16 2
increment 2: 1 2
18 32 12 2 38 33 16 5
Step 1) Look at 18, 12, 38, 16 and sort them in their appropriate location:
12 32 16 2 18 33 38 5
Step 2) Look at 32, 2, 33, 5 and sort them in their appropriate location:
12 2 16 5 18 32 38 33
Shellsort Examples (con’t)
Sort: 18 32 12 5 38 33 16 2
* floor(2/2) floor(1) = 1
increment 1: 1
12 2 16 5 18 32 38 33
2 5 12 16 18 32 33 38
void main()
{
cout<<"Actual Array : "; disp();
shellsort();
system("pause");
}
Output
Empirical Analysis of Shellsort
(Advantage)
• Advantage of Shellsort is that its only
efficient for medium size lists. For bigger
lists, the algorithm is not the best choice.
Fastest of all O(N^2) sorting algorithms.
• 5 times faster than the bubble sort and a
little over twice as fast as the insertion sort,
its closest competitor.
• The number of moves in the range ().
Empirical Analysis of Shellsort
(Disadvantage)
• It is a complex algorithm and its not nearly as
efficient as the merge, heap and quick sorts.
• It is significantly slower than the merge, heap and
quick sorts, but its relatively simple algorithm
makes it a good choice for sorting lists of less than
5000 items unless speed important.
Shellsort Best Case
• Best Case: The best case in the shell sort is
when the array is already sorted in the right
order. The number of comparisons is less
Shellsort Worst Case
• The running time of Shellsort depends on
the choice of increment sequence.
• The problem with Shell’s increments is that
pairs of increments are not necessarily
relatively prime and smaller increments can
have little effect.
Additional Online References
• Spark Notes (From Barnes & Noble):
– http://www.sparknotes.com/cs/