
























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Slides on different sorting algorithms such as Insertion Sort, Merge Sort, Quick Sort, and Counting Sort. It includes pseudo code and illustrative examples of how each algorithm works. Insertion Sort is typically done in-place, by iterating up the array, growing the sorted list behind it. Merge Sort divides the array in half and recursively sorts each half before merging them. Quick Sort divides the array into two sub-arrays and recursively sorts them. Counting Sort creates a count array to store count of individual elements and then modifies the input array.
Typology: Exams
1 / 64
This page cannot be seen from the preview
Don't miss anything!

























































for(int i=1; i<=length-1; i++) int j=i; while(j>0 and A(j-1) > A(j) ) swap A(j-1) and A(j) j=j- end while end for On your left, is the pseudo code for insertion sort. Below is an example of how this sorting technique works with actual array elements. Sorting is typically done in- place, by iterating up the array, growing the sorted list behind it.
A divide-and-conquer algorithm :
Mergesort(Pass an array) if array size > 1 Divide array in half Call Mergesort on first half. Call Mergesort on second half. Merge two halves. Merge(Pass two arrays) Compare leading element in each array Select lower and place in new array. (If one input array is empty then place remainder of other array in output array) MergeSort(A, left, right) { if (left < right) { mid = floor((left + right) / 2); MergeSort(A, left, mid); MergeSort(A, mid+1, right); Merge(A, left, mid, right); } } // Merge() takes two sorted subarrays of A and // merges them into a single sorted subarray of A // (how long should this take?)
Merge
Merge
Merge
Merge
Merge
Merge
Merge