Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 101 A Survey of Computer Science Sorting.

Similar presentations


Presentation on theme: "Computer Science 101 A Survey of Computer Science Sorting."— Presentation transcript:

1 Computer Science 101 A Survey of Computer Science Sorting

2 Sorting Some of the most heavily used algorithms are those for sorting data. Some of the most heavily used algorithms are those for sorting data. Arranging data into numerical or alphabetical order for purposes of Arranging data into numerical or alphabetical order for purposes of –Reports by category –Summarizing data –Searching data

3 Sorting (cont) Given: n, N1,N2,…,Nn Given: n, N1,N2,…,Nn Want: Rearrangement M1,M2,…,Mn where M1  …  Mn according to the appropriate understanding of . Want: Rearrangement M1,M2,…,Mn where M1  …  Mn according to the appropriate understanding of . There are many well-known algorithms for doing this. Preference may be due to There are many well-known algorithms for doing this. Preference may be due to –List size –Degree of order already present –Ease of programming –Program available

4 Sorting - Selection Sort Strategy: Strategy: –Find the largest element in list. –Swap it with last element in list. –Find next largest. –Swap it with next to last element in list –Etc. Variables: Variables: *U - Marker for unsorted section *L - Position of largest so far *C - Current position

5 Selection Sort - The algorithm Set U to n While U>1 Set L to 1 Set C to 2 While C ≤ U do If N(C) > N(L) then Set L to C Set C to C+1 end-of-loop Exchange N(L) and N(U) Set U to U-1 end-of-loop Stop Set U to n While U>1 Set L to 1 Set C to 2 While C ≤ U do If N(C) > N(L) then Set L to C Set C to C+1 end-of-loop Exchange N(L) and N(U) Set U to U-1 end-of-loop Stop

6 Selection Sort - Example (Pass 1) 6 5 19 912 UL C 6 519912 UL C 6 519912 UL C 6 519912 UL C 6 519912 U L C 6 5199 UL C 126 5 919 U L C 912

7 Selection Sort - Example (Pass 2) 6 512919 UL C 6 5129 UL C 196 59 UL C 126 9 UL C 19 5612919 UL C 56 51219 UL C 9

8 Selection Sort - Example (Pass 3) 6 5912 UL C 196 5912 UL C 19612 UL C 199 56 512 UL C 1996 5 LU C 12919

9 Selection Sort - Example (Pass 4) 6 512 UL C 1995 612 U 1996 512 UL C 199591219 LU C 6

10 Sorting - Bubble Sort Strategy: Strategy: –Pass through the list from left to right. –Compare each element with the one to its left. –Swap them if they are out of order. –Such a pass will put largest at the right end. –Continue making these passes until sorted. Variables: Variables: *U - Marker for unsorted section *C - Current position

11 Bubble Sort - The algorithm Set U to n While U > 1 do Set C to 2 While C ≤ U do If N(C) 1 do Set C to 2 While C ≤ U do If N(C) < N(C-1) then Exchange N(C) and N(C-1) Set C to C+1 end-of-loop Set U to U-1 end-of-loop Stop

12 Bubble Sort - Example (Pass 1) 5 91963 U C 9 5 63 U C 5 96 3 U C 5 6 U C 3 95 93 U C 6

13 Bubble Sort - Example (Pass 2) 5 963 U C 19 U 5 963 C C U 5 639 C U 5 693

14 Bubble Sort - Example (Pass 3) C U 5 63919 C U 5 639 C U 5 369

15 Bubble Sort - Example (Pass 4) C U 5 36919 C U 3 569 U 3 569

16


Download ppt "Computer Science 101 A Survey of Computer Science Sorting."

Similar presentations


Ads by Google