UNIT-5
SORTING TECHNIQUES
Sorting Techniques
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
1. Bubble sort
Bubble Sort is a simple-minded algorithm based on the idea that –
we look at the list, and wherever we find two consecutive elements out of order, we
swap them.
To do this :
Repeatedly traverse the unsorted part of the array,
comparing consecutive elements, and
Interchange them when they are out of order.
The name of the algorithm refers to the fact that the largest element "sinks" to the
bottom and the smaller elements "float" to the top.
Example : 4, 9, 5, 1, 0
Example (contd.)
Algorithm
int temp;
for(int i=0; i<a.length; i++)
{
int flag=0;
for(int j=0; j<a.length-1-i; j++)
{
if (a[j]>a[j+i])
{
temp=a[j];
a[j]=a[j+1]
a[j+1]=temp;
flag=1;
}
}
If (flag==0)
{
break;
}
}
2. SELECTION SORT
The Selection sort algorithm is based on the idea of finding the
minimum or maximum element in an unsorted array and then putting it
in its correct position in a sorted array.
Assume that the array A=[7,5,4,2] needs to be sorted in ascending order.
The minimum element in the array i.e. 2 is searched for and then
swapped with the element that is currently located at the first position,
i.e. 7. Now the minimum element in the remaining unsorted array is
searched for and put in the second position, and so on.
Example: 7, 5, 4, 2
PSUDOCODE
void sort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
Algorithm
Algo SelectionSort(a[ ])
{
int min, temp=0;
for (i=0; i<a.length; i++)
{
min=i;
for(int j=i+1; j<a.length; j++)
{
if (a[j] < a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
3. Insertion Sort
1. We start by making the second element of the given array, i.e. element at index 1,
the key.
2. We compare the key element with the element(s) before it, in this case, element at
index 0:
•If the key element is less than the first element, we insert the key element before the first element.
•If the key element is greater than the first element, then we insert it after the first element.
3. Then, we make the third element of the array as key and will compare it with elements
to it's left and insert it at the right position.
4. And we go on repeating this, until the array is sorted.
EXAMPLE: 4, 3, 2, 10, 12, 1, 5, 6
EXAMPLE: 11, 12, 13, 5, 6
Let us loop for i = 1 (second element of the array) to 4 (last element of the array)
• i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12
11, 12, 13, 5, 6
• i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than 13
11, 12, 13, 5, 6
• i = 3. 5 will move to the beginning and all other elements from 11 to 13 will move one position ahead of
their current position.
5, 11, 12, 13, 6
• i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one position ahead of their
current position.
5, 6, 11, 12, 13
Algorithm- Insertion Sort
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
4. Merge Sort
• Merge Sort uses a divide and conquer paradigm for sorting.
• It divides the problem into sub problems and solves them individually.
• It then combines the results of sub problems to get the solution of the original problem
Merge Sort Algorithm works in the following steps-
• It divides the given unsorted array into two halves- left and right sub arrays.
• The sub arrays are divided recursively.
• This division continues until the size of each sub array becomes 1.
• After each sub array contains only a single element then merge procedure is called.
• The merge procedure combines these trivially sorted arrays to produce a final sorted array.
Example
Merge Sort Pseudocode
mergesort(a, lb, ub)
{
if (lb < ub)
{
mid = (lb + ub) /2;
mergesort(a, lb, mid);
mergesort(a, mid+1, ub);
merge(a, lb, mid, ub);
}
}
Merge Pseudocode
merge(a, lb, mid, ub)
{
i=lb; j=mid+1; k=lb;
while (i<=mid && j<=ub)
{
if(a[i]<=a[j])
{
b[k]=a[i];
i++; k++;
}
else
{
b[k]=a[j];
j++; k++;
}
}
if (i>mid)
{
while (j<=ub)
{
b[k] = a[j];
j++;
k++;
}
}
else if (j>ub)
{
while (i<=mid)
{
b[k]=a[i];
i++;
k++;
}}
5. Quick Sort [10, 15, 1, 2, 9, 16, 11]
Quick Sort follows a recursive algorithm.
•It divides the given array into two sections using a partitioning
element called as pivot.
The division performed is such that-
•All the elements to the left side of pivot are smaller than pivot.
•All the elements to the right side of pivot are greater than pivot.
After dividing the array into two sections, the pivot is set at its correct
position.
Then, sub arrays are sorted separately by applying quick sort
algorithm recursively.
5. Quick Sort Pseudocode
partition(a, lb, ub)
{
pivot= a[lb];
right= ub;
left= lb;
while(left<right)
{
while(a[left]<=pivot)
{
left++;
}
while(a[right]>pivot)
{
right--;
}
if (left < right)
{swap(a[left], a[right]);}
}
swap(a[lb],a[right]);
return right;
}
quicksort(a, lb, ub)
{
if (lb<ub)
{
loc = partition(a, lb, ub);
quicksort(a, lb, loc-1);
quicksort(a, loc+1, ub);
}
}
Thank You

A Comprehensive Comparative Study and Performance Evaluation

  • 1.
  • 2.
    Sorting Techniques Bubble Sort SelectionSort Insertion Sort Merge Sort Quick Sort
  • 3.
    1. Bubble sort BubbleSort is a simple-minded algorithm based on the idea that – we look at the list, and wherever we find two consecutive elements out of order, we swap them. To do this : Repeatedly traverse the unsorted part of the array, comparing consecutive elements, and Interchange them when they are out of order. The name of the algorithm refers to the fact that the largest element "sinks" to the bottom and the smaller elements "float" to the top.
  • 4.
    Example : 4,9, 5, 1, 0
  • 5.
  • 6.
    Algorithm int temp; for(int i=0;i<a.length; i++) { int flag=0; for(int j=0; j<a.length-1-i; j++) { if (a[j]>a[j+i]) { temp=a[j]; a[j]=a[j+1] a[j+1]=temp; flag=1; } } If (flag==0) { break; } }
  • 7.
    2. SELECTION SORT TheSelection sort algorithm is based on the idea of finding the minimum or maximum element in an unsorted array and then putting it in its correct position in a sorted array. Assume that the array A=[7,5,4,2] needs to be sorted in ascending order. The minimum element in the array i.e. 2 is searched for and then swapped with the element that is currently located at the first position, i.e. 7. Now the minimum element in the remaining unsorted array is searched for and put in the second position, and so on.
  • 8.
  • 9.
    PSUDOCODE void sort(int arr[]) { intn = arr.length; for (int i = 0; i < n-1; i++) { // Find the minimum element in unsorted array int min_idx = i; for (int j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } }
  • 10.
    Algorithm Algo SelectionSort(a[ ]) { intmin, temp=0; for (i=0; i<a.length; i++) { min=i; for(int j=i+1; j<a.length; j++) { if (a[j] < a[min]) { min=j; } } temp=a[i]; a[i]=a[min]; a[min]=temp; }
  • 11.
    3. Insertion Sort 1.We start by making the second element of the given array, i.e. element at index 1, the key. 2. We compare the key element with the element(s) before it, in this case, element at index 0: •If the key element is less than the first element, we insert the key element before the first element. •If the key element is greater than the first element, then we insert it after the first element. 3. Then, we make the third element of the array as key and will compare it with elements to it's left and insert it at the right position. 4. And we go on repeating this, until the array is sorted.
  • 12.
    EXAMPLE: 4, 3,2, 10, 12, 1, 5, 6
  • 13.
    EXAMPLE: 11, 12,13, 5, 6 Let us loop for i = 1 (second element of the array) to 4 (last element of the array) • i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12 11, 12, 13, 5, 6 • i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than 13 11, 12, 13, 5, 6 • i = 3. 5 will move to the beginning and all other elements from 11 to 13 will move one position ahead of their current position. 5, 11, 12, 13, 6 • i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one position ahead of their current position. 5, 6, 11, 12, 13
  • 14.
    Algorithm- Insertion Sort voidsort(int arr[]) { int n = arr.length; for (int i = 1; i < n; ++i) { int key = arr[i]; int j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } }
  • 15.
    4. Merge Sort •Merge Sort uses a divide and conquer paradigm for sorting. • It divides the problem into sub problems and solves them individually. • It then combines the results of sub problems to get the solution of the original problem Merge Sort Algorithm works in the following steps- • It divides the given unsorted array into two halves- left and right sub arrays. • The sub arrays are divided recursively. • This division continues until the size of each sub array becomes 1. • After each sub array contains only a single element then merge procedure is called. • The merge procedure combines these trivially sorted arrays to produce a final sorted array.
  • 16.
  • 17.
    Merge Sort Pseudocode mergesort(a,lb, ub) { if (lb < ub) { mid = (lb + ub) /2; mergesort(a, lb, mid); mergesort(a, mid+1, ub); merge(a, lb, mid, ub); } }
  • 18.
    Merge Pseudocode merge(a, lb,mid, ub) { i=lb; j=mid+1; k=lb; while (i<=mid && j<=ub) { if(a[i]<=a[j]) { b[k]=a[i]; i++; k++; } else { b[k]=a[j]; j++; k++; } } if (i>mid) { while (j<=ub) { b[k] = a[j]; j++; k++; } } else if (j>ub) { while (i<=mid) { b[k]=a[i]; i++; k++; }}
  • 19.
    5. Quick Sort[10, 15, 1, 2, 9, 16, 11] Quick Sort follows a recursive algorithm. •It divides the given array into two sections using a partitioning element called as pivot. The division performed is such that- •All the elements to the left side of pivot are smaller than pivot. •All the elements to the right side of pivot are greater than pivot. After dividing the array into two sections, the pivot is set at its correct position. Then, sub arrays are sorted separately by applying quick sort algorithm recursively.
  • 20.
    5. Quick SortPseudocode partition(a, lb, ub) { pivot= a[lb]; right= ub; left= lb; while(left<right) { while(a[left]<=pivot) { left++; } while(a[right]>pivot) { right--; } if (left < right) {swap(a[left], a[right]);} } swap(a[lb],a[right]); return right; } quicksort(a, lb, ub) { if (lb<ub) { loc = partition(a, lb, ub); quicksort(a, lb, loc-1); quicksort(a, loc+1, ub); } }
  • 21.