15
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Sorting Course Lecture Slides 24 May 2010 “The real focus here is bringing some sort of order.” --Janet Hubbell

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

  • Upload
    astra

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010. Sorting. “The real focus here is bringing some sort of order.” --Janet Hubbell. Course Lecture Slides 24 May 2010. Ganesh Viswanathan. Sorting. Rearrange a[0], a[1], …, a[n-1] into ascending order. - PowerPoint PPT Presentation

Citation preview

Page 1: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

CIS3023: Programming Fundamentals for CIS Majors IISummer 2010

Ganesh Viswanathan

Sorting

Course Lecture Slides24 May 2010

“The real focus here is bringing some sort of order.” --Janet Hubbell

Page 2: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Sorting

1. Rearrange a[0], a[1], …, a[n-1] into ascending order.

2. When done, a[0] <= a[1] <= … <= a[n-1]

3. a[0], a[1],…a[n] could be integers, doubles, characters, or objects

Page 3: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Sorting Algorithms

1. Selection-based• Selection sort• Heap Sort

2. Insertion-based• Insertion Sort• Shell Sort

3. Exchange-Based• Bubble Sort• Quick Sort

4. Merge-based• Merge Sort

All these are comparison-based sorting algorithms.

Page 4: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Selection Sort Algorithm

• List is sorted by selecting a list element and moving it to its proper position.

• Algorithm finds position of smallest element and moves it to the top of unsorted portion of list.

• Repeats process above until entire list is sorted.

Page 5: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Selection Sort Algorithm (Cont’d)

Figure 1: An array of 10 elements

Figure 2: Smallest element of unsorted array

Page 6: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Selection Sort Algorithm (Cont’d)

Figure 3: Swap elements list[0] and list[7]

Figure 4: Array after swapping list[0] and list[7]

Page 7: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Selection Sort Algorithm (Cont’d)

In-class exercise:

Complete Selection Sort and get the array fully sorted!

Show at each step: # array elements, # comparisons required, and # exchanges performed.

Page 8: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Insertion Sort Algorithm

• Based on technique of card players to arrange a hand

• Player keeps cards picked up so far in sorted order

• When the player picks up a new card• Makes room for the new card• Then inserts it in its proper place

Page 9: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Example

Steps in Insertion Sort:

Inserting the FourthArray Element:

Page 10: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Bubble Sort Algorithm

Compares adjacent array elementsExchanges their values if they are out of order

Smaller values bubble up to the top of the arrayLarger values sink to the bottom

Page 11: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Bubble Sort Algorithm (Cont’d)

Figure 1: Elements of array list during the first iteration

Figure 2: Elements of array list during the second iteration

Page 12: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Bubble Sort Algorithm (Cont’d)

Figure 3: Elements of array list during the third iteration

Figure 4: Elements of array list during the fourth iteration

Page 13: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

“ To iterate is human, to recurse divine. ”

--L. Peter Deutsch

Page 14: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Merge Sort

2 9 5 4 8 1 6 7

2 9 5 4 8 1 6 7

split

2 9 split

5 4

2

split

9 5 4

8 1 6 7

8 1 6 7

2 9

merge

4 5 1 8 6 7

2 4 5 9 1 6 7 8

1 2 4 5 6 7 8 9

merge

merge

divide

conquer

Page 15: CIS3023: Programming Fundamentals for CIS Majors II Summer  2010

Merging two sorted lists

2 4 5 9

current1

1

1 6 7 8

current2

current3

(a) After moving 1 to temp (b) After moving all the elements in list2 to temp

to temp

2 4 5 9

current1

1 2 4 5 6 7 8 9

1 6 7 8

current2

current3

(c) After moving 9 to temp

2 4 5 9

current1

1 2 4 5 6 7 8

1 6 7 8

current2

current3