Upload
griffin-griffin
View
214
Download
0
Embed Size (px)
Citation preview
Data StructuresData Structures
Chapter 8Chapter 8SortingSorting
Andreas Savva
22
SortingSorting
SmithSanchez Roberts
KennedyJones
JohnsonJackson
Brown
George BrownGeorge Brown32 Cyprus Road32 Cyprus Road
Good GreenGood GreenLondon NW4 1NWLondon NW4 1NW
Records and their keysRecords and their keys
33
Sorting AlgorithmsSorting Algorithms The purpose of sort is to produce a list of elements The purpose of sort is to produce a list of elements
sorted in either ascending or descending order.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:
NameName SurnameSurname IDID GradeGrade
Sorting is often a very time-consuming action in a Sorting is often a very time-consuming action in a program, and therefore, program, and therefore, the choice of method used the choice of method used for searching for searching can make a substantial deference in can make a substantial deference in the program’s performance.the program’s performance.
Algorithms:Algorithms: Insertion sortInsertion sort Bubble sortBubble sort Quick sortQuick sort
44
998877665544332211
SortingSorting
55
Insertion SortInsertion Sort
Algorithm:Algorithm:
1.1. Put the first Put the first kk elements of a list in elements of a list in order.order.
2.2. Move the Move the k + 1k + 1 element into element into TempTemp..
3.3. Move the sorted elements (1 to k) Move the sorted elements (1 to k) down, one at a time, until the value in down, one at a time, until the value in TempTemp can be placed in order in the can be placed in order in the previously sorted portion of the list.previously sorted portion of the list.
66
TempTemp
Insertion Sort ExampleInsertion Sort Example
998877665544332211
K+1
77
Insertion Insertion Sort Sort
ExampleExample881515004422 22
881515002244 22TempTemp
881515004422 00
881515442200 00
881515442200 1515
881515442200 1515
881515442200 88
151588442200 88
88
Insertion Sort ProcedureInsertion Sort Procedurevoid 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; } }
99
Bubble SortBubble Sort
Algorithm:Algorithm:1.1. Starting at the beginning of the list each Starting at the beginning of the list each
time, successive passes are made time, successive passes are made through the list until it is sorted.through the list until it is sorted.
2.2. A flag is needed to indicate whether or A flag is needed to indicate whether or not an exchange is made during a given not an exchange is made during a given pass through the list.pass through the list.
3.3. Since each pass filters the largest (or Since each pass filters the largest (or smallest) element to the end of the list, smallest) element to the end of the list, the length of what remains to be sorted the length of what remains to be sorted can be decrease by 1 after each pass.can be decrease by 1 after each pass.
1010
5544332211
5544332211
5544332211
5544332211
5544332211
5544332211
5544332211
5544332211
1111
Bubble Sort Bubble Sort ExampleExample
882233001212
882233121200
882212123300
881212223300
121288223300
121288223300
121288332200
121288332200
11stst Pass Pass
121288223300
22ndnd Pass Pass
1212
Bubble Sort Example (continue)Bubble Sort Example (continue)
121288332200
121288332200
121288332200
33rdrd Pass Pass
No exchange was made in the 3No exchange was made in the 3rdrd pass, pass,thus the list is sorted and we STOP.thus the list is sorted and we STOP.
1313
Bubble Sort ProcedureBubble Sort Procedurevoid 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);}
1414
Quick SortQuick Sort Quick-sort is one of the fastest sorting techniques Quick-sort is one of the fastest sorting techniques
available. It uses available. It uses recursionrecursion and is based upon the and is based upon the idea of separating a list into to parts. One part contains idea of separating a list into to parts. One part contains items smaller than some item in the list called items smaller than some item in the list called PivotPivot; ; the other part contains items larger than the other part contains items larger than PivotPivot..
Algorithm:Algorithm:1.1. Select the element in the middle of the array (in position Select the element in the middle of the array (in position
(FIRST + LAST) DIV 2) to be the (FIRST + LAST) DIV 2) to be the pivotpivot value. value.2.2. Move all the items on the left position of the pivot position that 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 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 position, and all the items that they are smaller than the pivot value to the left of the pivot position. (The pivot can also value to the left of the pivot position. (The pivot can also change position).change position).
3.3. Split the table to two other tables and repeat steps 1 and 2 for Split the table to two other tables and repeat steps 1 and 2 for 1.1. FIRST = FIRST; LAST = Pivot Position – 1FIRST = FIRST; LAST = Pivot Position – 12.2. FIRST = Pivot Position + 1; LAST = LASTFIRST = Pivot Position + 1; LAST = LAST
1515Pivot
RightArrowRightArrow
Pivot
Pivot
Quick Sort ExampleQuick Sort Example
LeftArrow RightArrow
LeftArrow RightArrow
LeftArrowLeftArrow
1616
RightArrowRightArrow
Pivot
RightArrowRightArrow
Pivot
Quick Sort Example (continue)Quick Sort Example (continue)
RightArrowRightArrow
LeftArrowLeftArrow
LeftArrow RightArrowLeftArrow Pivot
RightArrowRightArrow
Pivot
LeftArrowLeftArrow
RightArrowRightArrow
LeftArrowPivot
Pivot
LeftArrowLeftArrow
1717
Pivot
RightArrowRightArrow
LeftArrowLeftArrow
Quick Sort Example (continue)Quick Sort Example (continue)
RightArrowRightArrow
Pivot
LeftArrowLeftArrow
RightArrowLeftArrow Pivot
1818
Quick Sort ProcedureQuick Sort Procedurevoid 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);}