50
Searching and Sorting Arrays

Searching and Sorting Arrays

  • Upload
    varick

  • View
    30

  • Download
    0

Embed Size (px)

DESCRIPTION

Searching and Sorting Arrays. Searching in ordered and unordered arrays. Find the minimal element in an unordered array. Steps: Initially, let the 0 th element be the minimal element. Then sweep through the array and see if we find something better. - PowerPoint PPT Presentation

Citation preview

Page 1: Searching and Sorting Arrays

Searching and Sorting Arrays

Page 2: Searching and Sorting Arrays

Searching in ordered and unordered arrays

Page 3: Searching and Sorting Arrays

Find the minimal element in an unordered array.

Steps:1. Initially, let the 0th element be the

minimal element.

2. Then sweep through the array and see if we find something better.

Page 4: Searching and Sorting Arrays

Find the minimal element in an unordered array.

//Initially, let the 0th element be// the minimal element.int whereSmallest = 0;//Then sweep through the array// and see if we find something better.

How?

Page 5: Searching and Sorting Arrays

Find the minimal element in an unordered array.

//Initially, let the 0th element be// the minimal element.int whereSmallest = 0;//Then sweep through the array// and see if we find something better.for (int i=1; i<A.length; i++) {

//check for something betterHow?

}

Page 6: Searching and Sorting Arrays

Find the minimal element in an unordered array.

//Initially, let the 0th element be// the minimal element.int whereSmallest = 0;//Then sweep through the array// and see if we find something better.for (int i=1; i<A.length; i++) {

//check for something betterif (A[i]<A[whereSmallest]) {

whereSmallest = i;}

}//at the end of the above loop, A[whereSmallest] is// the smallest element of A

Page 7: Searching and Sorting Arrays

Find the maximal element in an unordered array. What needs to be changed?

//Initially, let the 0th element be// the minimal element.int whereSmallest = 0;//Then sweep through the array// and see if we find something better.for (int i=1; i<A.length; i++) {

//check for something betterif (A[i]<A[whereSmallest]) {

whereSmallest = i;}

}//at the end of the above loop, A[whereSmallest] is// the smallest element of A

Page 8: Searching and Sorting Arrays

Find the maximal element in an unordered array. What needs to be changed?

//Initially, let the 0th element be// the minimal element.int whereLargest = 0;//Then sweep through the array// and see if we find something better.for (int i=1; i<A.length; i++) {

//check for something better

if (A[i]>A[whereLargest]) {whereLargest = i;

}}//at the end of the above loop, A[whereLargest] is// the largest element of A

Page 9: Searching and Sorting Arrays

What if the array is already sort?

If the array is sorted in ascending order, where is the minimal element? We could search it as before but there

is a better way. If the array is sorted in ascending

order, where is the maximal element?

Page 10: Searching and Sorting Arrays

What if the array is already sort?

If the array is sorted in descending order, where is the minimal element? We could search it as before but there

is a better way. If the array is sorted in descending

order, where is the maximal element?

Page 11: Searching and Sorting Arrays

Statistical median

From wikipedia:

“In probability theory and statistics, a median is a number dividing the higher half of a sample, a population, or a probability distribution from the lower half. The median of a finite list of numbers can be found by arranging all the observations from lowest value to highest value and picking the middle one. If there are an even number of observations, one often takes the mean of the two middle values.”

Given a sorted array, we can write a function that determines the median.

Page 12: Searching and Sorting Arrays

Statistical median

public static double median ( int[] A ) {//determine if the array length is// odd of evendouble result = 0;…return result;

}

Page 13: Searching and Sorting Arrays

Statistical median

public static double median ( int[] A ) {//determine if the array length is// odd of evendouble result = 0;if ((A.length%2)==0) {

//is this the odd or even case?…

} else {…

}return result;

}

Page 14: Searching and Sorting Arrays

Statistical median

public static double median ( int[] A ) {//determine if the array length is// odd of evendouble result = 0;if ((A.length%2)==0) {

//even case so calc mean of middle 2…

} else {//odd case so pick middle one… This case is easier.

}return result;

}

Page 15: Searching and Sorting Arrays

Statistical median

public static double median ( int[] A ) {//determine if the array length is// odd of evendouble result = 0;if ((A.length%2)==0) {

//even case so calc mean of middle 2…

} else {//odd case so pick middle oneresult = A[ A.length/2 ];

}return result;

}

Page 16: Searching and Sorting Arrays

Statistical median

public static double median ( int[] A ) {//determine if the array length is// odd of evendouble result = 0;if ((A.length%2)==0) {

//even case so calc mean of middle 2result = ( A[ A.length/2-1 ] + A[ A.length/2 ] )

/ 2.0;} else {

//odd case so pick middle oneresult = A[ A.length/2 ];

}return result;

}

Page 17: Searching and Sorting Arrays

Recap

So far we’ve:1. Found min/max elements in unsorted

and sorted arrays.2. Calculated median of sorted arrays.3. What if we would like to check

whether or not an array contains a specified value? What type of thing should this function

return?

Page 18: Searching and Sorting Arrays

Searching an unordered (unsorted) array for a specific element.

Page 19: Searching and Sorting Arrays

Unsorted search for specified element

public static boolean unsortedSearch ( int[] A, int what )

{…

}

Page 20: Searching and Sorting Arrays

Unsorted search for specified element

public static boolean unsortedSearch ( int[] A, int what )

{boolean found = false;//now search A for what…return found;

}

Page 21: Searching and Sorting Arrays

Unsorted search for specified element

public static boolean unsortedSearch ( int[] A, int what )

{boolean found = false;//now search A for whatfor (int i=0; i<A.length; i++) {

…}return found;

}

Page 22: Searching and Sorting Arrays

Unsorted search for specified element

public static boolean unsortedSearch( int[] A, int what )

{boolean found = false;//now search A for whatfor (int i=0; i<A.length; i++) {

if (A[i]==what) {found = true;

}}return found;

}

Page 23: Searching and Sorting Arrays

Unsorted search for specified element: another slightly more efficient way

public static boolean unsortedSearch ( int[] A, int what )

{//search A for whatfor (int i=0; i<A.length; i++) {

if (A[i]==what) {return true;

}}return false;

}

Page 24: Searching and Sorting Arrays

An analysis of these two methods

Let’s count the number of comparisons performed in:

the best case

the worst case

the average case

Page 25: Searching and Sorting Arrays

An analysis of these two methods

Method Apublic static boolean unsortedSearch

( int[] A, int what ){

boolean found = false;//now search A for whatfor (int i=0; i<A.length; i++) {

if (A[i]==what) {found = true;

}}return found;

}

Method Bpublic static boolean unsortedSearch

( int[] A, int what ){

//search A for whatfor (int i=0; i<A.length; i++) {

if (A[i]==what) {return true;

}}return false;

}

What are the number of comparisons for the best, worse, and average cases for each method?

Page 26: Searching and Sorting Arrays

An analysis of these two methods

Method Apublic static boolean unsortedSearch

( int[] A, int what ){

boolean found = false;//now search A for whatfor (int i=0; i<A.length; i++) {

if (A[i]==what) {found = true;

}}return found;

}

Best = N; worst = N; average = N.

Method Bpublic static boolean unsortedSearch

( int[] A, int what ){

//search A for whatfor (int i=0; i<A.length; i++) {

if (A[i]==what) {return true;

}}return false;

}

Best = 1; worst = N; average = N/2.

Page 27: Searching and Sorting Arrays

Searching a sorted array for a specified element.

We could treat the array as unsorted and search it but that would be inefficient.

So let’s introduce the binary search method.

Page 28: Searching and Sorting Arrays

Binary searchfirst middle last

Page 29: Searching and Sorting Arrays

Searching a sorted array for a specified element.

public static boolean sortedSearch( int[] A, int what, int first, int last )

{boolean foundIt = false;…return foundIt;

}

Page 30: Searching and Sorting Arrays

Searching a sorted array for a specified element.

public static boolean sortedSearch( int[] A, int what, int first, int last )

{boolean foundIt = false;//base case…return foundIt;

}

Page 31: Searching and Sorting Arrays

Searching a sorted array for a specified element.

public static boolean sortedSearch( int[] A, int what, int first, int last )

{boolean foundIt = false;//base caseif (first>=last) {

if (what==A[first])foundIt = true;

} else {…

}return foundIt;

}

Page 32: Searching and Sorting Arrays

Searching a sorted array for a specified element.

public static boolean sortedSearch( int[] A, int what, int first, int last )

{boolean foundIt = false;//base caseif (first>=last) {

if (what==A[first])foundIt = true;

} else {int middle = (first+last) / 2;…

}return foundIt;

}

Page 33: Searching and Sorting Arrays

Searching a sorted array for a specified element.

public static boolean sortedSearch ( int[] A, int what, int first, int last ){

boolean foundIt = false;//base caseif (first>=last) {

if (what==A[first])foundIt = true;

} else {int middle = (first+last) / 2;if (what==A[middle])

…else if (what<A[middle])

…else

…}return foundIt;

}

Page 34: Searching and Sorting Arrays

Searching a sorted array for a specified element.

public static boolean sortedSearch ( int[] A, int what, int first, int last ){

boolean foundIt = false;//base caseif (first>=last) {

if (what==A[first])foundIt = true;

} else {int middle = (first+last) / 2;if (what==A[middle])

foundIt = true;else if (what<A[middle])

foundIt = sortedSearch( A, what, first, middle-1 );else

foundIt = sortedSearch( A, what, middle+1, last );}return foundIt;

} An example of a recursive function (a function that may call itself).

Page 35: Searching and Sorting Arrays

Searching a sorted array for a specified element.

private static boolean sortedSearch ( int[] A, int what, int first, int last ){

boolean foundIt = false;//base caseif (first>=last) {

if (what==A[first])foundIt = true;

} else {int middle = (first+last) / 2;if (what==A[middle])

foundIt = true;else if (what<A[middle])

foundIt = sortedSearch( A, what, first, middle-1 );else

foundIt = sortedSearch( A, what, middle+1, last );}return foundIt;

}

public static boolean sortedSearch ( int[] A, int what ) {return sortedSearch( A, what, 0, A.length-1 );

}

An example of a “helper” function (a function that helps get the recursion started; not an “official” term).

Also an example of function overloading (an “official” term).

Page 36: Searching and Sorting Arrays

Searching a sorted array for a specified element.

private static boolean sortedSearch ( int[] A, int what, int first, int last ){

//base caseif (first>=last) {

if (what==A[first])return true;

} else {int middle = (first+last) / 2;if (what==A[middle])

return true;else if (what<A[middle])

return sortedSearch( A, what, first, middle-1 );else

return sortedSearch( A, what, middle+1, last );}return false;

}

public static boolean sortedSearch ( int[] A, int what ) {return sortedSearch( A, what, 0, A.length-1 );

}

Arguably simpler with returns.

Page 37: Searching and Sorting Arrays

Searching a sorted array for a specified element.

public static boolean sortedSearch ( int[] A, int what ){

if (A.length==0) return false; //empty!

int first = 0, last = A.length - 1;while (first<last) {

int middle = (first+last) / 2;if (what == A[middle]) return true;if (what < A[middle]) last = middle - 1;else first = middle +

1;}if (what==A[first]) return true;return false;

} Non recursive version.

Page 38: Searching and Sorting Arrays

Summary

Searching in an unordered array (of length N) requires us to search N elements in the worst case.

Searching in an ordered array (also of length N) requires us to search log2 elements in the worst case.

Page 39: Searching and Sorting Arrays

Summary

Searching in an unordered array (of length N) requires us to search N elements in the worst case. So searching 1,000,000,000 things requires

searching 1,000,000,000 things.

Searching in an ordered array (also of length N) requires us to search log2 elements in the worst case. So searching 1,000,000,000 things requires

searching only 30 things! At most!

Page 40: Searching and Sorting Arrays

Sorting an array: the selection sort

Page 41: Searching and Sorting Arrays

Sorting

An arrangement or permutation of data

May be either: ascending (non decreasing) descending (non increasing)

Page 42: Searching and Sorting Arrays

Selection sort

Based on the idea of repeatedly finding the minimal elements.

But first, how can we find the (single, most) minimal element in an array?

Page 43: Searching and Sorting Arrays

Selection sortHow can we find the (single, most) minimal element in an array?

…//let 0 be the location of the smallest element so farint whereSmallest = 0;

for (int i=1; i<A.length; i++) {if (A[i]<A[whereSmallest]) {

whereSmallest = i;}

}

System.out.println( "the smallest is " + A[whereSmallest]+ " which was located at position " + whereSmallest + "." );

Page 44: Searching and Sorting Arrays

Selection sort

Idea: Find the smallest in A[0]..A[ A.length-1 ]. Put that in A[0]. Then find the smallest in A[1]..A[ A.length-1 ]. Put that in A[1]. …

But first, let’s develop a swapPairs function that swaps a pair of elements denoted by a and b in some array, A.

<-1, 2, 4, -5, 12> <-5, 2, 4, -1, 12>

Page 45: Searching and Sorting Arrays

Selection sort

public static void swapPair ( … ) {…

}

What do we need in here to do the job (function parameters)?

Page 46: Searching and Sorting Arrays

Selection sort

public static void swapPair ( int[] A, int a, int b ){

…}

What do we need in here to do the job (function parameters)?

Page 47: Searching and Sorting Arrays

Selection sort

public static void swapPair ( int[] A, int a, int b ){

int temp = A[a];A[a] = A[b];A[b] = temp;

}

Page 48: Searching and Sorting Arrays

Selection sort

public static void swapPair ( int[] A, int a, int b ){

int temp = A[a];A[a] = A[b];A[b] = temp;

}

Why doesn’t this work?

public static void swapPair ( int a, int b ){

int temp = a;a = b;b = temp;

}

Page 49: Searching and Sorting Arrays

Selection sortIdea:

Find the smallest in A[0]..A[ A.length-1 ]. Put that in A[0]. Then find the smallest in A[1]..A[ A.length-1 ]. Put that in A[1].

…//let 0 be the location of the smallest element so farint whereSmallest = 0;for (int i=1; i<A.length; i++) {

if (A[i]<A[whereSmallest]) {whereSmallest = i;

}}swapPairs( A, 0, whereSmallest );

Page 50: Searching and Sorting Arrays

Selection sortIdea:

Find the smallest in A[0]..A[ A.length-1 ]. Put that in A[0]. Then find the smallest in A[1]..A[ A.length-1 ]. Put that in A[1].

…for (int j=0; j<A.length; j++) {

//let j be the location of the smallest element so farint whereSmallest = j;for (int i=j+1; i<A.length; i++) {

if (A[i]<A[whereSmallest]) {whereSmallest = i;

}}swap( A, j, whereSmallest );

}