Searching and Sorting Arrays - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

Searching and Sorting Arrays

Description:

Searching and Sorting Arrays – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 47
Provided by: George933
Learn more at: http://people.sju.edu
Category:

less

Transcript and Presenter's Notes

Title: Searching and Sorting Arrays


1
Searching and Sorting Arrays
2
  • Searching in ordered and unordered arrays

3
Find the minimal element in an unordered array.
  • Steps
  • Initially, let the 0th element be the minimal
    element.
  • Then sweep through the array and see if we find
    something better.

4
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?

5
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 i1 iltA.length i)
  • //check for something better
  • How?

6
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 i1 iltA.length i)
  • //check for something better
  • if (AiltAwhereSmallest)
  • whereSmallest i
  • //at the end of the above loop, AwhereSmallest
    is
  • // the smallest element of A

7
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 i1 iltA.length i)
  • //check for something better
  • if (AiltAwhereSmallest)
  • whereSmallest i
  • //at the end of the above loop, AwhereSmallest
    is
  • // the smallest element of A

8
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 i1 iltA.length i)
  • //check for something better
  • if (AigtAwhereLargest)
  • whereLargest i
  • //at the end of the above loop, AwhereLargest
    is
  • // the largest element of A

9
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?

10
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?

11
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.

12
Statistical median
  • public static double median ( int A )
  • //determine if the array length is
  • // odd of even
  • double result 0
  • return result

13
Statistical median
  • public static double median ( int A )
  • //determine if the array length is
  • // odd of even
  • double result 0
  • if ((A.length2)0)
  • //is this the odd or even case?
  • else
  • return result

14
Statistical median
  • public static double median ( int A )
  • //determine if the array length is
  • // odd of even
  • double result 0
  • if ((A.length2)0)
  • //even case so calc mean of middle 2
  • else
  • //odd case so pick middle one
  • This case is easier.
  • return result

15
Statistical median
  • public static double median ( int A )
  • //determine if the array length is
  • // odd of even
  • double result 0
  • if ((A.length2)0)
  • //even case so calc mean of middle 2
  • else
  • //odd case so pick middle one
  • result A A.length/2
  • return result

16
Statistical median
  • public static double median ( int A )
  • //determine if the array length is
  • // odd of even
  • double result 0
  • if ((A.length2)0)
  • //even case so calc mean of middle 2
  • result ( A A.length/2-1 A A.length/2
    )
  • / 2.0
  • else
  • //odd case so pick middle one
  • result A A.length/2
  • return result

17
Recap
  • So far weve
  • Found min/max elements in unsorted and sorted
    arrays.
  • Calculated median of sorted arrays.
  • What if we would like to check whether or not an
    array contains a specified value?
  • What type of thing should this function return?

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

19
Unsorted search for specified element
  • public static boolean unsortedSearch ( int A,
    int what )

20
Unsorted search for specified element
  • public static boolean unsortedSearch ( int A,
    int what )
  • boolean found false
  • //now search A for what
  • return found

21
Unsorted search for specified element
  • public static boolean unsortedSearch ( int A,
    int what )
  • boolean found false
  • //now search A for what
  • for (int i0 iltA.length i)
  • return found

22
Unsorted search for specified element
  • public static boolean unsortedSearch
  • ( int A, int what )
  • boolean found false
  • //now search A for what
  • for (int i0 iltA.length i)
  • if (Aiwhat)
  • found true
  • return found

23
Unsorted search for specified element another
slightly more efficient way
  • public static boolean unsortedSearch ( int A,
    int what )
  • //search A for what
  • for (int i0 iltA.length i)
  • if (Aiwhat)
  • return true
  • return false

24
An analysis of these two methods
  • Lets count the number of comparisons performed
    in
  • the best case
  • the worst case
  • the average case

25
An analysis of these two methods
  • Method A
  • Method B
  • public static boolean unsortedSearch
  • ( int A, int what )
  • boolean found false
  • //now search A for what
  • for (int i0 iltA.length i)
  • if (Aiwhat)
  • found true
  • return found
  • public static boolean unsortedSearch ( int A,
    int what )
  • //search A for what
  • for (int i0 iltA.length i)
  • if (Aiwhat)
  • return true
  • return false

What are the number of comparisons for the best,
worse, and average cases for each method?
26
An analysis of these two methods
  • Method A
  • Method B
  • public static boolean unsortedSearch
  • ( int A, int what )
  • boolean found false
  • //now search A for what
  • for (int i0 iltA.length i)
  • if (Aiwhat)
  • found true
  • return found
  • Best N worst N average N.
  • public static boolean unsortedSearch ( int A,
    int what )
  • //search A for what
  • for (int i0 iltA.length i)
  • if (Aiwhat)
  • return true
  • return false
  • Best 1 worst N average N/2.

27
Searching a sorted array for a specified element.
  • We could treat the array as unsorted and search
    it but that would be inefficient.
  • So lets introduce the binary search method.

28
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

29
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

30
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
  • if (firstgtlast)
  • if (whatAlast)
  • foundIt true
  • else
  • return foundIt

31
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
  • if (firstgtlast)
  • if (whatAlast)
  • foundIt true
  • else
  • int middle (firstlast) / 2
  • return foundIt

32
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
  • if (firstgtlast)
  • if (whatAlast)
  • foundIt true
  • else
  • int middle (firstlast) / 2
  • if (whatAmiddle)
  • else if (whatltAmiddle)
  • else
  • return foundIt

33
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
  • if (firstgtlast)
  • if (whatAlast)
  • foundIt true
  • else
  • int middle (firstlast) / 2
  • if (whatAmiddle)
  • foundIt true
  • else if (whatltAmiddle)
  • foundIt sortedSearch( A, what, first,
    middle-1 )
  • else
  • foundIt sortedSearch( A, what, middle1,
    last )
  • return foundIt

An example of a recursive function (a function
that may call itself).
34
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
  • if (firstgtlast)
  • if (whatAlast)
  • foundIt true
  • else
  • int middle (firstlast) / 2
  • if (whatAmiddle)
  • foundIt true
  • else if (whatltAmiddle)
  • foundIt sortedSearch( A, what, first,
    middle-1 )
  • else
  • foundIt sortedSearch( A, what, middle1,
    last )
  • return foundIt

An example of a helper function (a function
that helps get the recursion started not an
official term) and function overloading (an
official term).
35
Searching a sorted array for a specified element.
  • public static boolean sortedSearch ( int A, int
    what )
  • if (A.length0) return false //empty!
  • int first 0, last A.length - 1
  • while (firstltlast)
  • int middle (firstlast) / 2
  • if (what Amiddle) return true
  • if (what lt Amiddle) last middle 1
  • else first middle 1
  • if (what Alast) return true
  • return false

Non recursive version.
36
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.

37
  • Sorting an array the selection sort

38
Sorting
  • An arrangement or permutation of data
  • May be either
  • ascending (non decreasing)
  • descending (non increasing)

39
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?

40
Selection sort
  • How can we find the (single, most) minimal
    element in an array?
  • //let 0 be the location of the smallest element
    so far
  • int whereSmallest 0
  • for (int i1 iltA.length i)
  • if (AiltAwhereSmallest)
  • whereSmallest i
  • System.out.println( the smallest is
    AwhereSmallest
  • which was located at position
    whereSmallest . )

41
Selection sort
  • Idea
  • Find the smallest in A0..A A.length-1 .
  • Put that in A0.
  • Then find the smallest in A1..A A.length-1 .
  • Put that in A1.
  • But first, lets develop a swapPairs function
    that swaps a pair of elements denoted by a and b
    in some array, A.
  • lt-1, 2, 4, -5, 12gt ? lt-5, 2, 4, -1, 12gt

42
Selection sort
  • public static void swapPair ( )

What do we need in here to do the job (function
parameters)?
43
Selection sort
  • public static void swapPair ( int A, int a, int
    b )

What do we need in here to do the job (function
parameters)?
44
Selection sort
  • public static void swapPair ( int A, int a, int
    b )
  • int temp Aa
  • Aa Ab
  • Ab temp

45
Selection sort
  • Idea
  • Find the smallest in A0..A A.length-1 .
  • Put that in A0.
  • Then find the smallest in A1..A A.length-1 .
  • Put that in A1.
  • //let 0 be the location of the smallest element
    so far
  • int whereSmallest 0
  • for (int i1 iltA.length i)
  • if (AiltAwhereSmallest)
  • whereSmallest i
  • swapPairs( A, 0, whereSmallest )

46
Selection sort
  • Idea
  • Find the smallest in A0..A A.length-1 .
  • Put that in A0.
  • Then find the smallest in A1..A A.length-1 .
  • Put that in A1.
  • for (int j0 jltA.length j)
  • //let j be the location of the smallest element
    so far
  • int whereSmallest j
  • for (int ij1 iltA.length i)
  • if (AiltAwhereSmallest)
  • whereSmallest i
  • swap( A, j, whereSmallest )
Write a Comment
User Comments (0)
About PowerShow.com