Docsity

Sorted Vectors: Implementation and Comparison of Merge Sort and Quick Sort Algorithms, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
1
CS261–DataStructures
Sorting Vectors
Vector: Variations
• There are some useful variations on the basic Vector data
structure
• Sorting vectors Æsort the vector elements (and keep it
sorted as elements are added,changed, and removed)
• Applications of sorting vectors
Search
• An unsorted vector requires a linear search
• Sorted vector can use the “Divide and Conquer” concept
Ant
0
Bird
1
Cat
2
Fox
4
Goat
5
Horse
6
Lion
7
Newt
8
Rat
9
Dog
3
Snake
10
Sorted Vectors
• Data abstraction:
– Store elements in anindexed data structure
– Uses binary searchto maintain order for insert and delete
public class SortedVector implements Sorted, Bag,
FindMin, FindNth,
SortAlgorithm {
protected Indexed data;
protected Comparator test;
public SortedVector() { test = new DefaultComparator();
data = new Vector(); }
public SortedVector(Comparator c)
{ test = c; data = new Vector(); }
public SortedVector(Indexed v, Comparator c)
{ test = c; sort(v); data = v; }
.
.
.
protected int binarySearch(Object key) { … }
}
Why use composition rather
than inheritance?
Binary Search
• Divide and conquer
• Easy to understand – difficult to program perfectly:
protected int binarySearch(Object key) {
int low = 0;
int high = data.size();
while (low < high) {
int mid = (low + high) / 2;
if (test.compare(data.elementAt(mid), key) < 0)
low=mid+1;
else
high = mid;
}
return low;
}
Sorted Vectors: Sorted Interface
public interface Sorted extends Collection {
// NO NEW METHODS.
}
• Sorted interface guarantees:
1. Implementation of the Collection methods – by calling t he
corresponding methods for data
2. When enumerated, elements will beordered from “smallest” to
“largest”
3. Traverse and count duplicates to determine frequency
• How do we know we’ll get an enumeration in order?
– Underlying vectoris maintained in sequence – i.e., elements are
put in their correct place
– An indexed enumeration produces elementsin the index order
pf3
pf4
pf5

Partial preview of the text

Download Sorted Vectors: Implementation and Comparison of Merge Sort and Quick Sort Algorithms and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS 261 – Data Structures

Sorting Vectors

Vector: Variations

  • There are some useful variations on the basic Vector datastructure
  • Sorting vectorssorted as elements are added, changed, and removed) Æ sort the vector elements (and keep it
  • Applications of sorting vectors

Search

  • An unsorted vector requires a linear search
  • Sorted vector can use the “Divide and Conquer” concept

Ant 0 Bird 1 Cat 2 Dog 3 Fox 4 Goat 5 Horse 6 Lion 7 Newt 8 Rat 9 Snake 10

Sorted Vectors

  • Data abstraction:– Store elements in an indexed data structure
    • Uses binary search to maintain order for insert and delete public class SortedVector implements Sorted, Bag, protected Indexed data; FindMin,SortAlgorithm {^ FindNth, protected Comparator test;public SortedVector() { test = new DefaultComparator(); public SortedVector(Comparator{ data =test =^ newc)c; dataVector(); } = new Vector(); } public SortedVector(Indexed v,. { test = Comparator c)c; sort(v); data = v; } ..protected int binarySearch(Object key) { … } }

Why use composition ratherthan inheritance?

Binary Search

  • Divide and conquer
  • Easy to understand – difficult to program perfectly: protected int binarySearch(Object key) { int lowint high = 0;= data.size(); while (low < high) {int mid = (low + high) / 2; if (test.compare(data.elementAt(mid), key)low = mid + 1; < 0) elsehigh = mid; }return low; }

Sorted Vectors: public interface Sorted Sorted Interface extends Collection {

//} NO NEW METHODS.

  • Sorted interface guarantees:1. Implementation of the Collection methods – by calling the
    1. When enumerated, elements will be ordered from “smallest” tocorresponding methods for^ data
    2. Traverse and count duplicates to determine frequency“largest”
  • How do we know we’ll get an enumeration in order?– Underlying vector is maintained in sequence – i.e., elements are
    • put in their correct placeAn indexed enumeration produces elements in the index order

Sorted Vectors: Bag Interface

  • Why the– Commonly used in conjunction with the sorted vector data type Bag interface?
    • Useful for finding specific objects// Implementation of the Bag interface. public void addElement(Object obj){ data.addElementAt(obj, binarySearch()); } public boolean containsElement(Object obj){ int idxreturn idx < data.size() = binarySearch(obj); && Time complexity: data.elementAt(idx).equals(obj);^ }
  • addElement addElementAt: uses to insert (O( binarySearch n )) Æ O( to find index (O(log n ) n )) and then uses
  • containsElementthen checks returned index for equality (O(1)): uses binarySearch Æto find index (O(log O(log n ) n )) and

Sorted Vectors: public Object findElement(Object Bag Interface (cont.) obj) {

int idx = binarySearch(obj);if (idx < data.sizereturn data.elementAt(idx); && data.elementAt(idx).equals(obj)) }elsethrow new^ NoSuchElemenException(String.valueOf(obj));. publicint idx = binarySearch(obj);if (idx < data.size void removeElement(Object && data.elementAt(idx).equals(obj)) obj) { elsereturnthrow new^ data.elementAt(idx); NoSuchElemenException(String.valueOf(obj));. Time complexity:^ }

  • findElement binarySearch: returns the (O(log n )) elementAt Æ O(log n ) (O(1)) the index returned by
  • removeElement binarySearch : uses(O(log removeElementAt n )) Æ O( n ) (O( n )) the index returned by

Sorted Vectors: FindMin & FindNth Interface

  • Useful with sorted data:// Implementation of the FindMin interface. // Method addElement is shared with Bag interface public Objectpublic void removeFirst() getFirst() { data.elementAt(0); }{ data.removeElementAt(0); Æ already implemented. } // Implementation of the FindNth interface. public Object findNth(int idx){ return data.elementAt(idx); }

Time complexity: – getFirst: returns elementAt(0)Æ O(1)

  • – removeFirstfindNth: returns: uses elementAt(idx) removeElementAt(0)Æ O(1) Æ O( n )

Sorted Vectors: SortAlgorithm Interface

public void sort(Indexed data) { … }

  • Why have a sorting algorithm?– If initial indexed data (as passed to the constructor) is unsorted, must sort it immediately
  • Any sort will do, we will look at two that can be pluggedinto the sort routine:
    1. Merge Sort
    2. Quick Sort

Merge Sort

  • Merge sort takes advantage of the linear time it takes tomerge two sorted sequences into one
  • It uses a stack to hold the merged elements and thencopies them back into the original data

2 4 6 1 3 5 7

What’s next?^1 2

Merge Sort

  • Merge sort takes advantage of the linear time it takes tomerge two sorted sequences into one
  • It uses a stack to hold the merged elements and thencopies them back into the original data 2 4 6 1 3 5 7

1 112 11223 2213134 331241245 44353561212 5512346123467

Merge Sort public class MergeSort implements SortAlgorithm {

private Comparator test;private Indexedprivate Vector data;buf = new Vector(); public MergeSort() { test = new DefaultComparator(); }public MergeSort(Comparator t) { test = t; } public voiddata =mergeSort(0, v.size()); v; sort(Indexed v) {buf.ensureCapacity(v.size()); }private void mergeSort(int low, int high) { ... } }private void^ merge(int^ low, int mid, int high)^ { ... }

to compare.^ Tell it how Indexed data.Can sort^ any

Merge Sort private void merge(int low, int mid, int high) {

int ibuf.setSize(0);while = low,(i < mid || j j = mid; < high) { if (^) if (j <(i < mid) { high && test.compare(data.elementAt(j),data.elementAt(i) < 0) }elsebuf.addLast(data.element(j++));^ buf.addLast(data.element(i++)); }else buf.addLast(data.element(j++)); j for (i = = buf.size();data.setElement(buf.elementAt(i), low 0; i < (^) j; i++)// Copy buffer back to original area. + i); }

Partitioning

Many problems involve dividing a vector into two sectionsbased on a comparison to one element in the collection:

Basis for two algorithms: FindNth and Quicksort

Pivot

< Pivot Pivot >= Pivot

FindNth

  • We implementedjust returning the element at the FindNth with the n th (^) indexSortedVector by
  • What if we don’t want the overhead of sorting:– Don’t care about the order of the elements prior to (or after) the n th^ element
  • Partition the vector:– If pivot == n we’re done since everything to the left is smaller
    • Otherwise, partition the section containing the(ignoring the other section) n th^ element

FindNth public class NthVector implements findNth {

... public Object findNth(int idx) // All same as before. { if(idx < 0)) || (idxreturn findNth(n,throw NoSuchElementException(); 0, > data.size()))data.size()); }private Object findNth(int n, int low, int high) { int idx = if (idx == pivot(low, n) return high, (high+low)/2);data.elementAt(n); // Partition data.// Done? }^ if (n < idx) return findNth(n, low,^ return^ findNth(n,^ idx^ + 1, high);^ idx);^ // No: try again. }

FindNth: Time Complexity Analysis

  • FindNth is only a one-sided recursive call
  • A good pivot roughly divides the data in half at each step: n + n /2 + n /4 + … + 1 = O( n )
  • A bad pivotsingle element at each step: (i.e., largest or smallest value) only removes a n + ( n – 1) + ( n – 2) + … + 1 = O( n^2 )
  • On the average Æ very fast

QuickSort

  • Use partition algorithm, but recursively sort both sides
  • After the first partition, no elements will need to beswapped across the pivot
  • Each partition can be sorted independently: private void quickSort (int low, int high) { if (lowint idx >= high) return;= pivot(low, high, (low + high) / 2); quickSort(low, idx);quickSort(idx + 1, high); }

QuickSort: Time Complexity Analysis

  • Running time depends on pivot choice.
  • Best case if pivot breaks vector in half: O( n log n )
  • Worst case if pivot is largest or smallest value: n recursive calls * linear time partition Æ O( n (^2) )
  • Benchmarks indicate it’s faster than the other sortalgorithms we’ve studied so far

Pivot Selection

  • Heuristics…
  • Use the middle ( n / 2) element
  • Use the first element
  • Use a random element
  • Use the median value of first, middle and last elements

Quick Review

  • Vectors can grow unlike arrays and still provide randomaccess
  • Finding a particular object in a vector requires an O(search n )
  • Can do better if we sort the vectoruse a binary search (SortedVector) and
  • SortedVectortwo MergeSort and QuickSort requires a sorting algorithm … we saw
  • When should you use a Vector vs. a SortedVector?