Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Chapter 8 Sorting Andreas Savva. 2 Sorting Smith Sanchez Roberts Kennedy Jones Johnson Jackson Brown George Brown 32 Cyprus Road Good.

Similar presentations


Presentation on theme: "Data Structures Chapter 8 Sorting Andreas Savva. 2 Sorting Smith Sanchez Roberts Kennedy Jones Johnson Jackson Brown George Brown 32 Cyprus Road Good."— Presentation transcript:

1 Data Structures Chapter 8 Sorting Andreas Savva

2 2 Sorting Smith Sanchez Roberts Kennedy Jones Johnson Jackson Brown George Brown 32 Cyprus Road Good Green London NW4 1NW Records and their keys

3 3 Sorting Algorithms The purpose of sort is to produce a list of elements sorted in either ascending or descending order. 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: A list can be sorted by a different key: Name Name Surname Surname ID ID Grade 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. 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: Algorithms: Insertion sort Insertion sort Bubble sort Bubble sort Quick sort Quick sort

4 4 987654321 Sorting

5 5 Insertion Sort Algorithm: 1. Put the first k elements of a list in order. 2. Move the k + 1 element into Temp. 3. 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.

6 6 Temp Insertion Sort Example 987654321 K+1

7 7 Insertion Sort Example 8150422 815024 2Temp 8150420 8154200 81542015 81542015 8154208 1584208

8 8 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; }

9 9 Bubble Sort Algorithm: 1. Starting at the beginning of the list each time, successive passes are made through the list until it is sorted. 2. A flag is needed to indicate whether or not an exchange is made during a given pass through the list. 3. 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.

10 10 54321 54321 54321 54321 54321 54321 54321 54321

11 11 Bubble Sort Example 823012 823120 821230 812230 128230 128230 128320 128320 1 st Pass 128230 2 nd Pass

12 12 Bubble Sort Example (continue) 128320 128320 128320 3 rd Pass No exchange was made in the 3 rd pass, thus the list is sorted and we STOP.

13 13 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); }

14 14 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. 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: 1. Select the element in the middle of the array (in position (FIRST + LAST) DIV 2) to be the pivot value. 2. 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). 3. Split the table to two other tables and repeat steps 1 and 2 for 1. FIRST = FIRST; LAST = Pivot Position – 1 2. FIRST = Pivot Position + 1; LAST = LAST

15 15 Pivot RightArrow Quick Sort Example LeftArrow RightArrow LeftArrow RightArrow LeftArrow

16 16 RightArrow Pivot RightArrow Quick Sort Example (continue) RightArrow LeftArrow LeftArrow RightArrow LeftArrow Pivot RightArrow LeftArrow RightArrow LeftArrow Pivot LeftArrow

17 17 Pivot RightArrow LeftArrow Quick Sort Example (continue) RightArrow Pivot LeftArrow RightArrow LeftArrow Pivot

18 18 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); }


Download ppt "Data Structures Chapter 8 Sorting Andreas Savva. 2 Sorting Smith Sanchez Roberts Kennedy Jones Johnson Jackson Brown George Brown 32 Cyprus Road Good."

Similar presentations


Ads by Google