Computer Science 101Computer Science 101A Survey of Computer A Survey of Computer
ScienceScience
SortingSorting
SortingSorting Some of the most heavily used Some of the most heavily used
algorithms are those for sorting data.algorithms are those for sorting data.
Arranging data into numerical or Arranging data into numerical or alphabetical order for purposes ofalphabetical order for purposes of– Reports by categoryReports by category– Summarizing dataSummarizing data– Searching dataSearching data
Sorting (cont)Sorting (cont) Given: n, N1,N2,…,NnGiven: n, N1,N2,…,Nn Want: Rearrangement M1,M2,…,Mn Want: Rearrangement M1,M2,…,Mn
where M1where M1… … Mn according to the Mn according to the appropriate understanding of appropriate understanding of ..
There are many well-known algorithms There are many well-known algorithms for doing this. Preference may be due for doing this. Preference may be due toto– List sizeList size– Degree of order already presentDegree of order already present– Ease of programmingEase of programming– Program availableProgram available
Sorting - Selection SortSorting - Selection Sort Strategy:Strategy:
– Find the largest element in list.Find the largest element in list.– Swap it with last element in list.Swap it with last element in list.– Find next largest.Find next largest.– Swap it with next to last element in listSwap it with next to last element in list– Etc.Etc.
Variables:Variables:* U - Marker for unsorted sectionU - Marker for unsorted section* L - Position of largest so farL - Position of largest so far* C - Current positionC - Current position
Selection Sort - The Selection Sort - The algorithmalgorithm
Set U to nSet U to nWhile U>1 While U>1 Set L to 1 Set L to 1
Set C to 2Set C to 2While C While C ≤ U do≤ U do
If N(C) > N(L) thenIf N(C) > N(L) thenSet L to CSet L to C
Set C to C+1Set C to C+1end-of-loopend-of-loopExchange N(L) and N(U)Exchange N(L) and N(U)Set U to U-1Set U to U-1
end-of-loopend-of-loopStopStop
Selection Sort - Example Selection Sort - Example (Pass 1)(Pass 1)
6 5 19 9 12UL
C
6 5 19 9 12UL
C
6 5 19 9 12UL
C6 5 19 9 12
UL
C
6 5 19 9 12UL
C
6 5 19 9UL
C12
6 5 12 9 19UL
C
9 12
Selection Sort - Example (Pass Selection Sort - Example (Pass 2)2)
6 5 12 9 19UL
C
6 5 12 9UL
C19
6 5 9 19UL
C12
6 12 9UL
C19 5
6 12 9 19UL
C 5
6 5 12 19UL
C9
Selection Sort - Example Selection Sort - Example (Pass 3)(Pass 3)
6 5 9 12UL
C19
6 5 9 12UL
C 196 12
UL
C199 5
6 5 12UL
C199
6 5 19LU
C129 19
Selection Sort - Example Selection Sort - Example (Pass 4)(Pass 4)
6 5 12UL
C199 5 6 12
U 199
6 5 12UL
C199
5 9 12 19LU
C 6
Sorting - Bubble SortSorting - Bubble Sort Strategy:Strategy:
– Pass through the list from left to right.Pass through the list from left to right.– Compare each element with the one to its Compare each element with the one to its
left.left.– Swap them if they are out of order.Swap them if they are out of order.– Such a pass will put largest at the right Such a pass will put largest at the right
end.end.– Continue making these passes until sorted.Continue making these passes until sorted.
Variables:Variables:* U - Marker for unsorted sectionU - Marker for unsorted section* C - Current positionC - Current position
Bubble Sort - The Bubble Sort - The algorithmalgorithm
Set U to nSet U to nWhile U > 1 do While U > 1 do Set C to 2 Set C to 2
While C While C ≤ U do≤ U do If N(C) < N(C-1) then If N(C) < N(C-1) then
Exchange N(C) and Exchange N(C) and N(C-1)N(C-1)
Set C to C+1 Set C to C+1end-of-loopend-of-loopSet U to U-1Set U to U-1
end-of-loopend-of-loopStopStop
Bubble Sort - Example Bubble Sort - Example (Pass 1)(Pass 1)
5 9 19 6 3U
C
9 5 19 6 3U
C5 9 6 19 3
U
C
5 19 6U
C3 9
5 9 3 19U
C6
Bubble Sort - Example (Pass Bubble Sort - Example (Pass 2)2)
5 9 6 3U
C19
U5 9 6 3
C19
C
U5 6 3 9 19
C
U5 6 9 3 19
Bubble Sort - Example (Pass Bubble Sort - Example (Pass 3)3)
C
U5 6 3 9 19
C
U5 6 3 9 19
C
U5 3 6 9 19
Bubble Sort - Example (Pass Bubble Sort - Example (Pass 4)4)
C
U5 3 6 9 19
C
U3 5 6 9 19
U3 5 6 9 19