180 likes | 275 Views
Andreas Savva. Data Structures. Chapter 8 Sorting. Smith. Roberts. Jones. Jackson. Sanchez. Kennedy. Johnson. Brown. George Brown 32 Cyprus Road Good Green London NW4 1NW. Sorting. Records and their keys. Sorting Algorithms.
E N D
Andreas Savva Data Structures Chapter 8 Sorting
Smith Roberts Jones Jackson Sanchez Kennedy Johnson Brown George Brown 32 Cyprus Road Good Green London NW4 1NW Sorting Records and their keys
Sorting Algorithms • The purpose of sort is to produce a list of elements sorted in either ascending or descending order. • A list can be sorted by a different key: • Name • Surname • ID • Grade • Sorting is often a very time-consuming action in a program, and therefore, the choice of method used for searching can make a substantial deference in the program’s performance. • Algorithms: • Insertion sort • Bubble sort • Quick sort
1 2 3 4 5 6 7 8 9 Sorting
Insertion Sort Algorithm: • Put the first k elements of a list in order. • Move the k + 1 element into Temp. • Move the sorted elements (1 to k) down, one at a time, until the value in Temp can be placed in order in the previously sorted portion of the list.
Temp K+1 1 2 3 4 5 6 7 8 9 Insertion Sort Example
Temp 4 2 0 15 8 2 2 4 0 15 8 2 2 4 0 15 8 0 0 2 4 15 8 0 0 2 4 15 8 15 0 2 4 15 8 15 0 2 4 15 8 8 0 2 4 8 15 8 Insertion Sort Example
Insertion Sort Procedure void InsertionSort(List_entry List[], int size) { int i, k; List_entry temp; bool done; for (k = 1; k < size; k++) { temp = List[k]; i = k; done = false; while ((i > 0) && !done) if (temp < List[i – 1]) { List[i] = List[i – 1]; i--; } else done = true; List[i] = temp; } }
Bubble Sort Algorithm: • Starting at the beginning of the list each time, successive passes are made through the list until it is sorted. • A flag is needed to indicate whether or not an exchange is made during a given pass through the list. • Since each pass filters the largest (or smallest) element to the end of the list, the length of what remains to be sorted can be decrease by 1 after each pass.
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5
0 0 0 0 0 0 0 12 0 3 3 3 3 12 2 0 2 3 12 3 3 2 2 3 2 3 2 8 8 8 2 2 8 12 8 2 12 12 8 8 12 12 8 12 8 2nd Pass Bubble Sort Example 1st Pass
0 0 0 2 2 2 3 3 3 8 8 8 12 12 12 3rd Pass Bubble Sort Example (continue) No exchange was made in the 3rd pass, thus the list is sorted and we STOP.
Bubble Sort Procedure void BubbleSort(List_entry List[], int size) { bool exchange_made; size = size – 1; do { exchange_made = false; for (int i = 0; i < size; i++) if (List[i] > List[i + 1]) { swap(List[i], List[i + 1]); exchange_made = true; } size = size – 1; } while (exchange_made && (size > 0); }
Quick Sort • Quick-sort is one of the fastest sorting techniques available. It uses recursion and is based upon the idea of separating a list into to parts. One part contains items smaller than some item in the list called Pivot; the other part contains items larger than Pivot. Algorithm: • Select the element in the middle of the array (in position (FIRST + LAST) DIV 2) to be the pivot value. • Move all the items on the left position of the pivot position that they are bigger than the pivot value to the right of the pivot position, and all the items that they are smaller than the pivot value to the left of the pivot position. (The pivot can also change position). • Split the table to two other tables and repeat steps 1 and 2 for • FIRST = FIRST; LAST = Pivot Position – 1 • FIRST = Pivot Position + 1; LAST = LAST
Pivot Pivot RightArrow RightArrow RightArrow LeftArrow LeftArrow LeftArrow Pivot Quick Sort Example
Pivot Pivot Pivot RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow Pivot Pivot Pivot Quick Sort Example (continue)
Pivot RightArrow RightArrow RightArrow LeftArrow LeftArrow LeftArrow Pivot Pivot Quick Sort Example (continue)
Quick Sort Procedure void QuickSort(List_entry List[], int Left, int Right) { int Pivot, LeftArrow = Left, RightArrow = Right; Pivot = List[(Left + Right) / 2]; do { while (List[LeftArrow] <= Pivot) LeftArrow++; while (List[RightArrow] >= Pivot) RightArrow--; if (LeftArrow < RightArrow) Swap(List[LeftArrow++], List[RightArrow--]); } while (LeftArrow < RightArrow) if (Left < RightArrow) QuickSort(List, Left, RightArrow); if (LeftArrow < Right) QuickSort(List, LeftArrow, Right); } main() { List_entry A[MAX]; int size; . . . QuickSort(A, 0, size – 1); }