16
Computer Science 101 Computer Science 101 A Survey of Computer A Survey of Computer Science Science Sorting Sorting

Computer Science 101 A Survey of Computer Science

  • Upload
    ketan

  • View
    46

  • Download
    1

Embed Size (px)

DESCRIPTION

Computer Science 101 A Survey of Computer Science. Sorting. Sorting. Some of the most heavily used algorithms are those for sorting data. Arranging data into numerical or alphabetical order for purposes of Reports by category Summarizing data Searching data. Sorting (cont). - PowerPoint PPT Presentation

Citation preview

Page 1: Computer Science 101 A Survey of Computer Science

Computer Science 101Computer Science 101A Survey of Computer A Survey of Computer

ScienceScience

SortingSorting

Page 2: Computer Science 101 A Survey of Computer Science

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

Page 3: Computer Science 101 A Survey of Computer Science

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

Page 4: Computer Science 101 A Survey of Computer Science

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

Page 5: Computer Science 101 A Survey of Computer Science

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

Page 6: Computer Science 101 A Survey of Computer Science

Selection Sort - Example Selection Sort - Example (Pass 1)(Pass 1)

6 5 19 9 12

UL

C

6 5 19 9 12

UL

C

6 5 19 9 12

UL

C

6 5 19 9 12

UL

C

6 5 19 9 12

UL

C

6 5 19 9

UL

C

12

6 5 12 9 19

UL

C

9 12

Page 7: Computer Science 101 A Survey of Computer Science

Selection Sort - Example (Pass Selection Sort - Example (Pass 2)2)

6 5 12 9 19

UL

C

6 5 12 9

UL

C

19

6 5 9 19

UL

C

12

6 12 9

UL

C

19 5

6 12 9 19

UL

C

5

6 5 12 19

UL

C

9

Page 8: Computer Science 101 A Survey of Computer Science

Selection Sort - Example Selection Sort - Example (Pass 3)(Pass 3)

6 5 9 12

UL

C

19

6 5 9 12

UL

C

196 12

UL

C

199 5

6 5 12

UL

C

199

6 5 19

LU

C

129 19

Page 9: Computer Science 101 A Survey of Computer Science

Selection Sort - Example Selection Sort - Example (Pass 4)(Pass 4)

6 5 12

UL

C

199 5 6 12

U

199

6 5 12

UL

C

199

5 9 12 19

LU

C

6

Page 10: Computer Science 101 A Survey of Computer Science

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

Page 11: Computer Science 101 A Survey of Computer Science

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

Page 12: Computer Science 101 A Survey of Computer Science

Bubble Sort - Example Bubble Sort - Example (Pass 1)(Pass 1)

5 9 19 6 3

U

C

9 5 19 6 3

U

C

5 9 6 19 3

U

C

5 19 6

U

C

3 9

5 9 3 19

U

C

6

Page 13: Computer Science 101 A Survey of Computer Science

Bubble Sort - Example (Pass Bubble Sort - Example (Pass 2)2)

5 9 6 3

U

C

19

U

5 9 6 3

C

19

C

U

5 6 3 9 19

C

U

5 6 9 3 19

Page 14: Computer Science 101 A Survey of Computer Science

Bubble Sort - Example (Pass Bubble Sort - Example (Pass 3)3)

C

U

5 6 3 9 19

C

U

5 6 3 9 19

C

U

5 3 6 9 19

Page 15: Computer Science 101 A Survey of Computer Science

Bubble Sort - Example (Pass Bubble Sort - Example (Pass 4)4)

C

U

5 3 6 9 19

C

U

3 5 6 9 19

U

3 5 6 9 19

Page 16: Computer Science 101 A Survey of Computer Science