0% found this document useful (0 votes)
157 views

11 Sorting

The document discusses various sorting algorithms including selection sort, insertion sort, and bubble sort. It provides pseudocode examples and analyzes each algorithm based on efficiency, requirements on data type, space usage, stability, and performance on nearly sorted data. The analysis shows that selection sort, insertion sort, and bubble sort have quadratic time complexity of O(n^2) in the worst case, though insertion sort and bubble sort can perform better on nearly sorted data.

Uploaded by

api-3799621
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views

11 Sorting

The document discusses various sorting algorithms including selection sort, insertion sort, and bubble sort. It provides pseudocode examples and analyzes each algorithm based on efficiency, requirements on data type, space usage, stability, and performance on nearly sorted data. The analysis shows that selection sort, insertion sort, and bubble sort have quadratic time complexity of O(n^2) in the worst case, though insertion sort and bubble sort can perform better on nearly sorted data.

Uploaded by

api-3799621
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Sorting

COMP 103 #11


Menu
• Binary Search
• Sorting
• approaches
• selection sort
• insertion sort
• bubble sort
• Analysis

COMP103 2006/T3 2
Log2(n ) :
The number of times you can divide a set of n things in half.
lg(1000) =10, lg(1,000,000) = 20, lg(1,000,000,000) =30
Every time you double n, you add one step to the cost!

• Arises all over the place in analysing algorithms


“Divide and Conquer” algorithms (easier to solve small
problems):
Problem

Solve Solve

Solution

COMP103 2006/T3 3
ArraySet with Binary
Search
ArraySet: unordered
• All cost in the searching: O(n)
• contains: O(n )
• add: O(n )
• remove: O(n )

ArraySet: with Binary Search


• Binary Search is fast: O(log(n ))
• contains: O(log(n ))
• add: O(log(n )) O(n )
• remove: O(log(n )) O(n )

• All the cost is in keeping it sorted!!!!

COMP103 2006/T3 4
Making SortedArraySet fast
• If you have to call add() and/or remove() many items,
then SortedArraySet is no better than ArraySet (we’ll see
how to do better later in the course)
• Both O(n )
• Either pay to search
• Or pay to keep it in order

• If you only have to construct the set once, and then many
calls to contains(),
then SortedArraySet is much better than ArraySet.
• SortedArraySet contains() is O(log(n )

• But, how do you construct the set fast?


• A separate constructor.

COMP103 2006/T3 5
Alternative Constuctor
public SortedArraySet(Collection<E> c){

// Make space
count=c.size();
data = (E[]) new Object[count];

// Put collection into a list and sort


List<E> temp = new ArrayList<E>(c);
Collections.sort(temp, new ComparableComparator());

// Put sorted list into the data array


int i=0;
for (E item : temp)
      data[i++] = item;
}

• How do you actually sort?

COMP103 2006/T3 6
Introduction to Sorting
• The General Sorting Problem.
• We are given:
• A list.
• Items are all of the same type.
• A comparison function.
• That, given two list items, determine which should
come first.
• And using this function is the only way we can make
such a determination.
• We return:
• A sorted list with the same items as the original list.

COMP103 2006/T3 7
Ways of Sorting:
• Selecting sorts (Selection):
• Find the next largest/smallest item and put in place
• Builds the correct list in order

• Inserting Sorts (Insertion):


• For each item, insert it into an ordered sublist
• Builds a sorted list, but keeps changing it

• Compare and Swap Sorts (Bubble):


• Find two items that are out of order, and swap them
• Keeps “improving” the list

• Radix Sorts
• Look at the item and work out where it should go.
• Only works on some kinds of values.
COMP103 2006/T3 8
Selecting Sorts

Consumes wild when bear often humans a

• Selection Sort (slow)


• HeapSort (fast)

COMP103 2006/T3 9
Inserting Sorts

Consumes wild when bear often humans a

• Insertion Sort (slow)


• Shell Sort (pretty fast)
• Merge Sort (fast) – Divide and Conquer

COMP103 2006/T3 10
Compare and Swap Sorts

Consumes wild when bear often humans a

• Bubble Sort (slow)


• Quick Sort (fast) – Divide and Conquer

COMP103 2006/T3 11
Other Sorts

Consumes wild when bear often humans a

• Radix Sort (only works on some data)


• Permutation Sort (very slow) – Generate and Test

COMP103 2006/T3 12
Analysis of Sorting
• We can analyze sorting algorithms according to five criteria:
• Efficiency
• What is the (worst-case) order of the algorithm?
• Is the algorithm much faster on average than its worst-case performance?
• Requirements on Data
• Does the algorithm need random-access data?
• What operations does the algorithm require the data to have?
• Of course, we always need “compare”. What else?
• Space Usage
• Can the algorithm sort in-place?
• In-place means the algorithm does not copy the data to a separate buffer.
• How much additional storage is used?
• Every algorithm uses at least a little.
• Stability
• Is the algorithm stable?
• A sorting algorithm is stable if it does not reverse the order of equivalent items.
• Performance on Nearly Sorted Data
• Is the algorithm faster when given sorted (or nearly sorted) data?
• All items close to where they should be, or a limited number of items out of order.

COMP103 2006/T3 13
Sorting Algorithms
• There is no known sorting algorithm that has all
the properties we would like one to have.
• We will examine a number of sorting algorithms.
Generally, these fall into two categories:
• Quadratic [O(n2)] Algorithms
• Bubble Sort
• Selection Sort
• Insertion Sort
• Quicksort
• Log-Linear [O(n log n)] Algorithms
• Merge Sort
• Heap Sort

COMP103 2006/T3 14
What to sort?
• Could sort Lists
⇒ general and flexible
• but efficiency depends on how the List is implemented

• Could sort Arrays.


⇒ less general ?
• but efficiency is well defined
• easy to convert any Collection to an array
toArray() method.

• Comparing items:
• require items to be comparable
• provide comparable
• handle both.

COMP103 2006/T3 15
Selection Sort
public void selectionSort(E[ ] data, int size, Comparator<E>
comp){
  // for each position, from 0 up, find the next smallest item
// and swap it into place.
  for (int i=0; i<size-1; i++){
      int minIndex = i;
      for (int j=i+1; j<size; j++){
        if (comp.compare(data[j], data[minIndex]) < 0)
         minIndex=j;
   }
      if(i != minIndex) swap(data, i, minIndex);
 }
} Consumes wild when bear often humans a

COMP103 2006/T3 16
Selection Sort: Analysis
• Efficiency 
• Selection Sort is O(n2).
• Selection Sort also has an average-case time of O(n2). 
• Requirements on Data 
• Selection Sort does not require random-access data.
• Operations needed: swap.
• Space Usage 
• Selection Sort can be done in-place.
• It does not use significant additional storage.
• Stability 
• Selection Sort is stable.
• As long as we are careful in our implementation!
• Performance on Nearly Sorted Data 
• Not significantly faster on nearly sorted data: O(n2).
COMP103 2006/T3 17
Insertion Sort
public void insertionSort(E[] data, int size, Comparator<E>
comp){
// for each item, from 0, insert into place in the sorted region
(0..i-1)
for (int i=1; i<size; i++){
  E item = data[i];
  int place = i;                   
   while (place > 0 && comp.compare(item, data[place-1])
< 0){ 
      data[place] = data[place-1];
        place--;
   }
    data[place]= item;
} Consumes wild when bear often humans a

}
COMP103 2006/T3 18
Insertion Sort: Analysis
• Efficiency 
• Insertion Sort is O(n2).
• Insertion Sort also has an average-case time of O(n2). 
• Requirements on Data 
• Insertion Sort does not require random-access data.
• Operations needed: find in sorted list, remove, insert in sorted list.
• Space Usage 
• Insertion Sort can be done in-place.
• It does not use significant additional storage.
• Stability 
• Insertion Sort is stable.
• Performance on Nearly Sorted Data 
• Insertion Sort can be written to be O(n) if only a constant number of
items are out of place.
• Insertion Sort can be written to be O(n) if each item is at most some
constant distance from where it “should” be.

COMP103 2006/T3 19
Bubble Sort
• Scan through the list
• Compare and swap adjacent elements if not in order
• ‘Bubbles’ the largest value to the last position.

public void bubbleSort(E[ ] data, int size, Comparator<E> comp){ 

for (int pos=data.length-1; pos >= 0; pos--)


    for (int scan = 0; scan <= pos -1; scan++)
        if (comp.compare(data[scan], data[scan+1]) > 0)
         swap(data, scan, scan+1);
}

Consumes wild when bear often humans a

COMP103 2006/T3 20
Bubble Sort — Analysis
• Efficiency 
• Bubble Sort is O(n2).
• Bubble Sort also has an average-case time of O(n2). 
• Requirements on Data 
• Bubble Sort can work on data types without random access.
• Operations needed: (compare and) swap of consecutive items.
• Space Usage 
• Bubble Sort can be done in-place.
• It does not require significant additional storage.
• Stability 
• Bubble Sort is stable.
• Performance on Nearly Sorted Data /
• Bubble Sort is close to O(n) on nearly sorted data.

COMP103 2006/T3 21

You might also like