Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort

Similar presentations


Presentation on theme: "Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort"— Presentation transcript:

1 Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
Sorting Algorithms Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort

2 Bubble Sort Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. Worst and Average Case Time Complexity: O(n*n). Worst case occurs when array is reverse sorted. Best Case Time Complexity: O(n). Best case occurs when array is already sorted. Auxiliary Space: O(1) Boundary Cases: Bubble sort takes minimum time (Order of n) when elements are already sorted. Sorting In Place: Yes Stable: Yes

3 Usage Due to its simplicity, bubble sort is often used to introduce the concept of a sorting algorithm. In computer graphics it is popular for its capability to detect a very small error (like swap of just two elements) in almost-sorted arrays and fix it with just linear complexity (2n). For example, it is used in a polygon filling algorithm, where bounding lines are sorted by their x coordinate at a specific scan line (a line parallel to x axis) and with incrementing y their order changes (two elements are swapped) only at intersections of two lines

4 Bubble Sort Program #include<iostream> using namespace std; void swap(int *, int *); void bubbleSort(int []); int const n = 5; int arr[n]; void main() { for(int i = 0; i < n; i++) cin >> arr[i]; bubbleSort(arr); cout << arr[i] << "\t"; cout << endl; system("pause"); } void bubbleSort(int arr[]) { for(int i = 0; i < n-1; i++) for(int j = 0; j < n-1-i; j++) if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]); } void swap(int *a, int *b) int temp = *a; *a = *b; *b = temp;

5 Selection Sort The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. The subarray which is already sorted. Remaining subarray which is unsorted. In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.

6 Usage Time Complexity: O(n*n) as there are two nested loops.
Auxiliary Space: O(1) The good thing about selection sort is that it never makes more than O(n) swaps and can be useful when memory write is a costly operation.

7 Selection Sort Program
#include<iostream> using namespace std; void swap(int *, int *); void selectionSort(int []); int const n = 5; int arr[n]; void main() { for(int i = 0; i < n; i++) cin >> arr[i]; selectionSort(arr); cout << arr[i] << "\t"; cout << endl; system("pause"); } void selectionSort(int arr[]) { int out, in, min; for(out = 0; out < n-1; out++) min = out; for(in = out + 1; in < n; in++) if(arr[in] < arr[min]) min = in; swap(arr[min], arr[out]); } void swap(int *a, int *b) int temp = *a; *a = *b; *b = temp;

8 Insertion Sort Insertion Sort sorts an array by finding the next available unsorted number and inserting it into the sorted region of the array. In every iteration after insertion of unsorted element into it’s place other elements of the array will shifted to the end of array

9 Usage Time Complexity: O(n*n) Auxiliary Space: O(1)
Boundary Cases: Insertion sort takes maximum time to sort if elements are sorted in reverse order. And it takes minimum time (Order of n) when elements are already sorted. Algorithmic Paradigm: Incremental Approach Sorting In Place: Yes Stable: Yes Online: Yes Uses: Insertion sort is used when number of elements is small. It can also be useful when input array is almost sorted, only few elements are misplaced in complete big array.

10 Insertion Sort Program
#include<iostream> using namespace std; void insertionSort(int []); int const n = 5; int arr[n]; void main() { for(int i = 0; i < n; i++) cin >> arr[i]; insertionSort(arr); cout << arr[i] << "\t"; cout << endl; system("pause"); } void insertionSort(int arr[]) { int j; int temp; for(int i=1; i<n; i++) temp=arr[i]; for(j=i-1; (j>=0)&&(arr[j]>temp); j--) arr[j+1]=arr[j]; } arr[j+1]=temp;

11 Merge Sort MergeSort is a Divide and Conquer algorithm.
It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merg() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.

12 Pseudo-code MergeSort(arr[], l, r) If r > l
1.Find the middle point to divide the array into two halves: middle m = (l+r)/2 2. Call mergeSort for first half: Call mergeSort(arr, l, m) 3. Call mergeSort for second half: Call mergeSort(arr, m+1, r) 4. Merge the two halves sorted in step 2 and 3: Call merge(arr, l, m, r)

13

14 main() #include<iostream> using namespace std; void mergeSort(int*, int, int); void mergeArray(int*, int, int, int); int const n=6; void main() { int arr[n]; for(int i=0; i<n; i++) cin>>arr[i]; mergeSort(arr, 0, n-1); system("pause"); }

15 void mergesort() void mergeSort(int *arr, int startIndex, int endIndex) { int mid=(startIndex+endIndex)/2; if(startIndex < endIndex) mergeSort(arr, startIndex, mid); mergeSort(arr, mid+1, endIndex); mergeArray(arr, startIndex, endIndex, mid); }

16 void mergeArray() void mergeArray (int *arr, int startIndex, int endIndex, int mid) { int i, j, k, tempArray[n]; i=startIndex; j=mid+1; k=startIndex; while(i<=mid && j<=endIndex) { if(arr[i] < arr[j]) { tempArray[k] = arr[i]; k++; i++; } else { tempArray[k] = arr[j]; j++; while(i <= mid) { tempArray[k] = arr[i]; k++; i++; } while(j <= endIndex) tempArray[k] = arr[j]; j++; for(i=startIndex; i<k; i++) arr[i] = tempArray[i];

17 Usage Merge Sort is useful for sorting linked lists in O(nLogn) time. Other nlogn algorithms like Heap Sort, Quick Sort (average case nLogn) cannot be applied to linked lists. Inversion Count Problem Used in External Sorting

18 Quick Sort Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways. 1) Always pick first element as pivot. 2) Always pick last element as pivot (implemented below) 3) Pick a random element as pivot. 4) Pick median as pivot.

19 Definition cont. The key process in quickSort is partition().
Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.

20 Partition Algorithm There can be many ways to do partition
The logic is simple: start from the leftmost element and keep track of index of smaller (or equal to) elements as i. While traversing, if we find a smaller element, we swap current element with arr[i]. Otherwise we ignore current element.

21 Quick Sort Program #include<iostream> using namespace std; void swap(int*, int*); int partition (int [], int, int); void quickSort(int [], int, int); int const n=5; int arr[n]; void main() { for(int i = 0; i < n; i++) cin>>arr[i]; quickSort(arr, 0, n-1); cout << arr[i] << "\t"; cout << endl; system("pause"); } void quickSort(int arr[], int l, int h) { if (l < h) int p = partition(arr, l, h); quickSort(arr, l, p - 1); quickSort(arr, p + 1, h); }

22 Quick Sort Program int partition (int arr[], int l, int h) { int x = arr[h]; int i = (l - 1); for (int j = l; j <= h- 1; j++) if (arr[j] <= x) i++; swap(arr[i], arr[j]); } swap(arr[i + 1], arr[h]); return (i + 1); void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; }

23 Usage Although the worst case time complexity of QuickSort is O(n2) which is more than many other sorting algorithms like Merge Sort and Heap Sort, QuickSort is faster in practice, because its inner loop can be efficiently implemented on most architectures, and in most real-world data. QuickSort can be implemented in different ways by changing the choice of pivot, so that the worst case rarely occurs for a given type of data. However, merge sort is generally considered better when data is huge and stored in external storage.


Download ppt "Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort"

Similar presentations


Ads by Google