CS 112 Introduction to Programming - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

CS 112 Introduction to Programming

Description:

CS 112 Introduction to Programming Sorting of an Array Debayan Gupta Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry_at_cs.yale.edu – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 39
Provided by: Richard1570
Learn more at: https://zoo.cs.yale.edu
Category:

less

Transcript and Presenter's Notes

Title: CS 112 Introduction to Programming


1
CS 112 Introduction to Programming
  • Sorting of an Array
  • Debayan Gupta
  • Computer Science Department
  • Yale University
  • 308A Watson, Phone 432-6400
  • Email yry_at_cs.yale.edu

2
Sorting
3
Roadmap Arrays
  • Motivation, declaration, initialization, access
  • Reference semantics arrays as objects
  • Example usage of arrays
  • Tallying array elements as counters
  • Keeping state
  • Manipulating arrays
  • Sorting an array

4
Sorting an Array
  • The process of arranging an array of elements
    into some order, say increasing order, is called
    sorting
  • Many problems require sorting
  • Google display from highest ranked to lower
    ranked
  • Morse code

5
Sorting in CS
  • Sorting is a classical topic in algorithm design

6
Sorting an Array
  • How do we sort an array of numbers?

int numbers 3, 9, 6, 1,
2
7
Many Sorting Algorithms
  • Insertion sort
  • Selection sort
  • Bubble sort
  • Merge sort
  • Quick sort

http//www.youtube.com/watch?vINHF_5RIxTE
8
Insertion Sort
  • Basic idea divide and conquer (reduction)
  • reduce sorting n numbers to
  • sort the first n-1 numbers
  • insert the n-th number to the sorted first n-1

9
0
1
2
3
4
insertPos 4
insertPos 3
insertPos 2
insertPos 1
10
Insertion PseudoCode
  • // assume 0 to n 1 already sorted
  • // now insert numbersn
  • // insertPos n
  • // repeat (number at insertPos-1 gt
    to_be_inserted)
  • // shift larger values to the right
  • // numbersinsertPos lt- numbersinsertPos-1
  • // insertPos--
  • // numbersinsertPos lt- to_be_inserted

11
0
1
2
3
// insertPos n // repeat (number at
insertPos-1 gt to_be_inserted) // shift
larger values to the right //
numbersinsertPos lt- numbersinsertPos-1 //
insertPos-- // numbersinsertPos lt-
to_be_inserted
12
Refinement Insertion PseudoCode
  • // assume 0 to n 1 already sorted
  • // now insert numbersn
  • // insertPos n
  • // repeat (insertPos gt 0 number at
    insertPos-1 gt to_be_inserted)
  • // shift larger values to the right
  • // numbersinsertPos lt- numbersinsertPos-1
  • // insertPos--
  • // numbersinsertPos lt- to_be_inserted

13
Insertion Sort Implementation
  • public static void sort (int numbers)
  • for (int n 1 n lt numbers.length index)
  • int key numbersn
  • int insertPos n
  • // invariant the elements from 0 to index
    -1 // are already sorted. Insert the element
    at // index to this sorted sublist
  • while (insertPos gt 0 numbersinsertPos-1
    gt key)
  • // shift larger values to the right
  • numbersinsertPos numbersinsertPos-1
  • insertPos--
  • numbersinsertPos key
  • // end of for
  • // end of sort

14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
Analysis of Insertion Sort
  • What is algorithm complexity in the worst case?

int numbers 3, 9, 6, 1,
2
21
Sorting Arrays Bubble Sort
  • Scan the array multiple times
  • during each scan, if elements at i and i1 are
    out of order, we swap them
  • This sorting approach is called bubble sort
  • http//en.wikipedia.org/wiki/Bubble_sort
  • Remaining question when do we stop (the
    termination condition)?

22
Sorting Bubble Sort
  • public static void sort (int numbers)
  • boolean outOfOrder false
  • do
  • outOfOrder false
  • // one scan
  • for (int i 0 i lt numbers.length-1 i)
  • if (numbersi gt numbersi1) // out
    of order
  • // swap
  • int x numbersi
  • numbersi numbersi1
  • numbersi1 x
  • outOfOrder true
  • // end of if
  • // end of for
  • while (outOfOrder)
  • // end of sort

23
Selection Sort
  • For the i-th iteration, we select the i-th
    smallest element and put it in its final place in
    the sort list

24
Selection Sort
  • The approach of Selection Sort
  • select one value and put it in its final place in
    the sort list
  • repeat for all other values
  • In more detail
  • find the smallest value in the list
  • switch it with the value in the first position
  • find the next smallest value in the list
  • switch it with the value in the second position
  • repeat until all values are placed

25
Selection Sort
  • An example
  • original 3 9 6 1 2
  • smallest is 1 1 9 6 3 2
  • smallest is 2 1 2 6 3 9
  • smallest is 3 1 2 3 6 9
  • smallest is 6 1 2 3 6 9

26
Sorting Selection Sort
  • public static void sort (int numbers)
  • int min, temp
  • for (int i 0 i lt numbers.length-1 i)
  • // identify the i-th smallest element
  • min i
  • for (int scan i1 scan lt numbers.length
    scan)
  • if (numbersscan lt numbersmin)
  • min scan
  • // swap the i-th smallest element with that
    at i
  • temp numbersmin
  • numbersmin numbersi
  • numbersi temp
  • // end of for
  • // end of sort

27
Analysis of Selection Sort
  • What is algorithm complexity in the worst case?

int numbers 3, 9, 6, 1,
2
28
Roadmap
  • Both insertion and selection have complexity of
    O(N2)
  • Q What is the best that one can do and can we
    achieve it?

29
Sorting Merge Sort
  • Split list into two parts
  • Sort them separately
  • Combine the two sorted lists (Merge!)
  • Divide and Conquer!

30
Sorting
public void sort(int values) numbers
values // numbers has been previously declared
mergesort(0, number - 1) private void
mergesort(int low, int high) // check if
low is smaller then high, if not then the array
is sorted if (low lt high) // Get the
index of the element which is in the middle
int middle low (high - low) / 2
mergesort(low, middle) // Sort the left side of
the array mergesort(middle 1,
high) // Sort the right side of the array
merge(low, middle, high) // Combine them
both
31
Merging
private void merge(int low, int middle, int high)
// Copy both parts into the helper array
for (int i low i lt high i)
helperi numbersi int i low, j
middle 1, k low // Copy the smallest
values from either side while (i lt middle
j lt high) if (helperi lt helperj)
numbersk helperi i else
numbersk helperj j
k // Copy the rest of the left
side of the array into the target array while
(i lt middle) numbersk helperi
k i
32
Merging
3
4
6
7
1
5
8
2
33
Sorting Quick Sort
  • Select a random element
  • Compare it to every other element in your list to
    find out its rank or position
  • You have now split the list into two smaller
    lists (if a gt x and x gt b, then we know that a gt
    b we dont need to compare!)

34
Quicksort
private void quicksort(int low, int high)
int i low, j high // Get the pivot
element from the middle of the list int pivot
numberslow (high-low)/2 // Divide
into two lists while (i lt j) // If
the current value from the left list is smaller
then the pivot // element then get the next
element from the left list while
(numbersi lt pivot) i
// If the current value from the right list is
larger then the pivot // element then get
the next element from the right list while
(numbersj gt pivot) j--
35
Quicksort .. Contd.
// If we have found a values in the left
list which is larger then // the pivot
element and if we have found a value in the right
list // which is smaller then the pivot
element then we exchange the // values.
// As we are done we can increase i and j
if (i lt j) exchange(i, j)
i j-- // Recursion
if (low lt j) quicksort(low, j) if
(i lt high) quicksort(i, high)
private void exchange(int i, int j) int
temp numbersi numbersi numbersj
numbersj temp
36
Whats the best we can do?
  • N2?
  • N log N
  • Why?

37
N log N
  • N! possible outcomes
  • If we compare two numbers, there are only 2
    possible combinations that we can get
  • So, if we have x steps, then we can produce a
    total of 2x combinations
  • To get 2x gt N!, we need x gt N log N

38
Questions?
Write a Comment
User Comments (0)
About PowerShow.com