Ceng-112 Data Structures I Chapter 11 Sorting
Ceng-112 Data Structures I Figure 11-1
Ceng-112 Data Structures I Figure 11-2 Buble sort and straight insertion sort are stable, all of the others are unstable.
Ceng-112 Data Structures I It uses two pieces of data to sort: sorted and unsorted. In each pass of a sort, one or more pieces of data are inserted into their correct location. 1.The straight insertion sort 2.The shell sort Insertion Sort
Ceng-112 Data Structures I Figure 11-3 Insertion Sort Straight Insertion Sort It will take at most n-1 passes to sort the data. It uses two pieces of data to sort: sorted and unsorted. In each pass, the first element of the the unsorted sublist is transfered to the sorted sublist by insertion it at appropiate place.
Ceng-112 Data Structures I Figure 11-4 Insertion Sort Straight Insertion Sort
Ceng-112 Data Structures I algorithm insertionSort( ref list, val last ) Sort list[1..last] using insertion sort. The array is divided into sorted and unsorted lists. With each pass, the first element of unsorted list is inserted into sorted list. Pre List must contain at least one element. last is an index to last element in the list. Post List has been rearranged. Insertion Sort Straight Insertion Sort
Ceng-112 Data Structures I current = 2 2.loop (current <= last) 1.hold = list[current] 2.walker = current -1 3.loop (walker >=1 AND hold.key < list[walker].key) list[walker+1] = list[walker] walker = walker -1 4.list[walker +1] = hold 5.current = current +1 3.return end insertionSort Insertion Sort Straight Insertion Sort Inside loop is quadraticly dependent to outer loop and outer loop executes n-1 times. f(n)=n((n+1)/2) O(n 2 )
Ceng-112 Data Structures I The creator of this algorithm is Donald L.Shell. The list is divided into K segments, where K is known as the increment value. Each segment contains N/K or less elemets. After each pass through, the data in each segment are ordered. If there is only one segment, then the list is sorted. Insertion Sort Shell Sort
Ceng-112 Data Structures I Figure 11-5 Insertion Sort Shell Sort K = 3, three segments and K is an increment value. Each segment includes N/K or less elements. If there is only one segment, then the list is sorted
Ceng-112 Data Structures I Figure 11-6, a Insertion Sort Shell Sort k=last/2 k = 10/2=5
Ceng-112 Data Structures I Figure 11-6, b Insertion Sort Shell Sort k=5/2
Ceng-112 Data Structures I Figure 11-6, c and d Insertion Sort Shell Sort k=3/2
Ceng-112 Data Structures I A[1]A[1 + K] A[1 + 2K] A[1 + 3K] Sorted Unsorted Insertion Sort Shell Sort
Ceng-112 Data Structures I algorithm shellSort( ref list, val last ) Sort list[1], list[2],..,list[last] are sorted in place. After the sort, their keys will be in order, list[1].key <= list[2].key <=..<=list[last].key. Pre List is an unordered array of records. last is an index to last record in array. Post List is ordered on list[i].key. Insertion Sort Shell Sort
Ceng-112 Data Structures I incre = last /2 2.loop (incre not 0) 1.current = 1 + incre 2.loop (current <= last) 1.hold = list[current] 2.walker = current – incre 3.loop (walker >= 1 AND hold.key < list[walker].key) list[walker+incre] = list[walker] walker = walker – incre 4.list[walker + incre] = hold 5.current = current incre = incre /2 3.return end shellSort Insertion Sort Shell Sort log 2 n[(n-n/2)+(n-n/4)+..+1]=n.log 2 n O(n.log 2 n) We still need to include the third loop! O(n 1.25 ) executes log 2 n n - increment times each time n = n/2
Ceng-112 Data Structures I Selection Sort We simply select the smallest item in the list and place it in a sorted list. These steps are repeated until all of data have been sorted. 1.Straight Selection Sort 2.Heap Sort
Ceng-112 Data Structures I Figure 11-8 Selection Sort Straight Selection Sort The list is divided into two sublists, sorted and unsorted. Select the smallest element from unsorted sublist and exchange it with the element at the beginning of the unsorted data. The wall between two sublists moves one element.
Ceng-112 Data Structures I Figure 11-9 Selection Sort Straight Selection Sort
Ceng-112 Data Structures I algorithm selectionSort( ref list, val last ) Sort list[1..last] by selecting smallest element in unsorted portion of array and exchanging it with element at the beginning of the unordered list. Pre List is must contain at least one item. last is an index to last record in array. Post List has been rearranged smallest to largest. Selection Sort Straight Selection Sort
Ceng-112 Data Structures I current = 1 2.loop (current < last) 1.smallest = current 2.walker = current +1 3.loop (walker <= last) 1.if ( list[walker] < list[smallest]) smallest = walker 2.walker = walker + 1 smallest selected: exchange with current element! exchange (list, current, smallest) current = current return end selectionSort Selection Sort Straight Selection Sort O(n 2 ) executes n – 1 times
Ceng-112 Data Structures I Figure Selection Sort Heap Sort
Ceng-112 Data Structures I Figure Selection Sort Heap Sort
Ceng-112 Data Structures I Exchange Sort Exchange sorting, contains: 1.The most common sort in computer science; the buble sort. 2.The most efficient general purpose sort; the quick sort.
Ceng-112 Data Structures I Figure Exchange Sort Bubble Sort The list is divided into two sublists; sorted and unsorted. The smallest element is bubbled from the unsorted to the sorted list. The wall moves one element to the right. This sort requires n-1 passes to sort the data.
Ceng-112 Data Structures I Figure Exchange Sort Bubble Sort current walker
Ceng-112 Data Structures I algorithm bubleSort( ref list, val last ) Sort an array, list[1..last] using buble sort.Adjacent elements are compared and exchanged until list is completely ordered. Pre List is must contain at least one item. last is an index to last record in array. Post List has been rearranged smallest to largest. Exchange Sort Bubble Sort
Ceng-112 Data Structures I current = 1 sorted = false loop (current <= last AND sorted false) 1.walker = last 2.sorted = true 3.loop (walker > current) 1.if (list[walker] < list[walker-1]) sorted = false exchange (list, walker, walker-1) 2.walker = walker – 1 4.current = current return end bubleSort Exchange Sort Bubble Sort f(n) = n((n+1)/2) O(n 2 ) executes n times executes (n+1)/2 times
Ceng-112 Data Structures I Exchange Sort In the buble sort, consecutive items are compared and possibly exchanged on each pass through the list, which means that many exchanges may be needed to move an element to its correct position. Quick sort is exchanged involves elements that are far apart so that fewer exchanges are required to correctly position an element.
Ceng-112 Data Structures I Hw - 12 Create an array has 40 elements which are selected randomly between 0 and Print the unsorted array. Use the shell sort to sort the array. Print the sorted array elements again. Load your HW-12 to FTP site until 01 June 07 at 09:00.
Ceng-112 Data Structures I Exchange Sort Quick Sort Selects an element which is called as “pivot” for each iteration. Divides the list into three groups. 1.The elements of first group which has key values less then key of pivot. 2.The pivot element. 3.The elements of first group which has key values greater then key of pivot. The sorting continues by quick sorting the left partition followed by a quick sort of the right partition.
Ceng-112 Data Structures I Figure Exchange Sort Quick Sort > >
Ceng-112 Data Structures I Figure >= Exchange Sort Quick Sort 78 >62 84 >62 45 < 62 From right! 21 < < > 62 From left!
Ceng-112 Data Structures I Figure Exchange Sort Quick Sort Operation
Ceng-112 Data Structures I algorithm quickSort( ref list, val left, val right ) An array, list[left..right] is sorted using recursion. Pre List is an array of data to be sorted. left and right identify the first and last elements of the list respectively. Post the list is sorted. Exchange Sort Quick Sort
Ceng-112 Data Structures I if ((right – left) > minsize)// quick sort medianLeft( list, left, right) pivot = list(left) sortLeft = left +1 sortRight = right loop (sortLeft <= sortRight) (find keys on left that belongs on right) loop ((list[sortLeft].key < pivot.key)AND(sortLeft < sortRight)) sortLeft = sortLeft loop ((list[sortRight].key >= pivot.key)AND(sortRight<sortLeft)) sortRight = sortRight – 1 2.if (sortLeft < sortRight) 1.Exchange(list, sortLeft, sortRight) 2.sortLeft = sortLeft sortRight = sortRight – 1 (prepare for next phase) list[left] = list[sortLeft – 1] list[sortLeft – 1] = pivot if (left < sortRight) quickSort(list, left, sortRight-1) 1.if (sortLeft < right) 1.quickSort(list, sortLeft, right) else insertionSort(list, left, right) end quickSort O(n.log 2 n)
Ceng-112 Data Structures I Quick Sort medianLeft Algorithm algorithm medianLeft(ref sortData, val left, val right ) Find the median value of an array, sortData[left, right], and place it in the location sortData[left]. Pre sortData is an array of at least three elements left and right are the boundaries of the array. Post median value located and placed at sortData[left]. Rearrange sortData so median value is in the middle location. 1.mid = (left + right) / 2 2.if (sortData[left].key > sortData[mid].key) exchange(sortData, left, mid) 3.if (sortData[left].key > sortData[right].key) exchange(sortData, left, right) 4.if (sortData[mid].key > sortData[right].key) exchange(sortData, mid, right) (Median in inthe middle location, exchange it with left). exchange(sortData, left, mid) Return end medianLeft
Ceng-112 Data Structures I algorithm quickInsertion( ref list, val first, val last ) Sort list[1..last] using insertion sort. The array is divided into sorted and unsorted lists. With each pass, the first element of unsorted list is inserted into sorted list. Pre List must contain at least one element. first is an index to the first element in the list. last is an index to last element in the list Post List has been rearranged. Quick Sort quick Insertion Algorithm
Ceng-112 Data Structures I current = first +1 2.loop (current <= last) 1.hold = list[current] 2.walker = current -1 3.loop (walker >=first AND hold.key < list[walker].key) list[walker+1] = list[walker] walker = walker -1 4.list[walker +1] = hold 5.current = current +1 3.return end quickInsertion Quick Sort quick Insertion Algorithm
Ceng-112 Data Structures I Quick Sort Example
Ceng-112 Data Structures I Merge Sort
Ceng-112 Data Structures I Merge Sort
Ceng-112 Data Structures I
Ceng-112 Data Structures I
Ceng-112 Data Structures I Merge Sort
Ceng-112 Data Structures I Merge Sort
Ceng-112 Data Structures I
Ceng-112 Data Structures I
Ceng-112 Data Structures I Merging Pseudocode
Ceng-112 Data Structures I
Ceng-112 Data Structures I
Ceng-112 Data Structures I
Ceng-112 Data Structures I
Ceng-112 Data Structures I
Ceng-112 Data Structures I Counting Sort The counting sort is a linear algorithm to sort a list. The pseudocode algorith is: Arrays: A[1,..,n] original unsorted array B[1,..,n] array to hold sorted output C[1,..,k] working array to hold counts Counting_Sort(A, B, k) 1.// initialize the count array 2.for i=1 to k 3. C[i]=0 4.for j=1 to length[A] 5. C[ A[j] ]=C[ A[j] ] // C[i] now contains the number of elements equal to k. 7.for i=2 to k 8. C[i] = C[i] + C[i-1] 9.// C[i] now contains the number of elements less than or equal to i. 10.for j= length[A] downto B[ C[ A[j] ] ] = A[j] 12. C[ A[j] ] = C[ A[j] ] – 1 13.// Array B holds now the sorted sequence.
Ceng-112 Data Structures I Sample for Counting Sort for i=1 to k C[i]=0 for j=1 to length[A] C[ A[j] ]=C[ A[j] ] + 1
Ceng-112 Data Structures I Sample for Counting Sort for j= length[A] downto 1 B[ C[ A[j] ] ] = A[j] C[ A[j] ] = C[ A[j] ] – 1 for i=2 to k C[i] = C[i] + C[i-1]
Ceng-112 Data Structures I Sample for Counting Sort for j= length[A] downto 1 B[ C[ A[j] ] ] = A[j] C[ A[j] ] = C[ A[j] ] – 1
Ceng-112 Data Structures I Sample for Counting Sort
Ceng-112 Data Structures I Lineer time sorting algorithm and no comparisons needed. Assume n elements in range 1 to k. When k = O(n), running time is O(n). Counting Sort