8 views

Uploaded by Marxlen Apostol Bedolido

- Understanding Machine Learning
- DSA
- Closest Pair
- compact-data-structures-practical-approach.epub
- FastDTW Paper
- qb2018_EEM303
- Verizon
- PP Lab Manual
- DSChapter9.pdf
- New Microsoft Word Document
- Ch5-Searching-Part1-The Design and Analysis of Parallel Algori - Selim G Akl
- Bagshawe's - Proudlock's Egg Bag and Four Race Presentations
- c8-4
- Apr13certsdreport
- Internal Sorting Methods
- Convex Hull
- Bubble sort presentation
- Art of Programming Contest SE for Uva PDF
- Data Structures
- MCA-II Practical List Winter 2014 New

You are on page 1of 60

Chapter outline

sequential and binary search sorting shuffling custom ordering with Comparators

Program efficiency

2

reading: 13.1

Sequential search

int index = words.indexOf(word); if (index >= 0) { System.out.println(word + " is word #" + index); } else { System.out.println(word + " is not found."); }

sequential search: One that examines each element of a list in sequence until it finds the target value or reaches the end of the list.

index 0 1 2 3 4 5 6 ... value It was the best of times it ...

binary search: An algorithm that searches for a value in a sorted list by repeatedly eliminating half the list from consideration.

Can be written iteratively or recursively implemented in Java as method Arrays.binarySearch in java.util package

Start out with the search area being from indexes 0 to length-1. Examine the element in the middle of the search area.

If it is K, stop. Otherwise,

If it is smaller than K, eliminate the upper half of the search area. If it is larger than K, eliminate the lower half of the search area. Repeat the above examination.

5

Using binarySearch

// binary search on an array: int[] numbers = {-3, 2, 8, 12, 17, 29, 44, 58, 79}; int index = Arrays.binarySearch(numbers, 29); System.out.println("29 is found at index " + index);

// binary search on ArrayList with the same values: int index = Collections.binarySearch(list, 29); System.out.println("29 is found at index " + index);

Note that the values in the array / list are in sorted order. If they are not, binarySearch is not guaranteed to work properly.

7

Sorting

sorting: Rearranging the values in a list into a given order (often into their natural ascending order).

One of the fundamental problems in computer science Many sorts are comparison-based (must determine order through comparison operations on the input data)

index 0 1 2 3 4 5 6 7

value 15 2 8 1 17 10 12 5 index 0 1 2 3 4 5 6 7

value 1 2 5 8 10 12 15 17

// demonstrate the Arrays.sort method String[] strings = {"c", "b", "g", "h", "d", "f", "e", "a"}; System.out.println(Arrays.toString(strings)); Arrays.sort(strings); System.out.println(Arrays.toString(strings));

Output:

[c, b, g, h, d, f, e, a] [a, b, c, d, e, f, g, h]

Shuffling

Java has a shuffle method for a List, Collections.shuffle: String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"}; ArrayList<String> deck = new ArrayList<String>(); for (String rank : ranks) { // build sorted deck for (String suit : suits) { deck.add(rank + " of " + suit); } } Collections.shuffle(deck); System.out.println("Top card = " + deck.get(0));

10

reading: 13.1

11

Custom ordering

Example: The following code sorts the strings in a casesensitive order, so the uppercase letters come first. We may have wanted a case-insensitive ordering instead.

String[] strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"}; Arrays.sort(strings); System.out.println(Arrays.toString(strings));

You can describe a custom sort ordering by creating a class called a comparator.

12

Comparators

The Comparator interface in java.util describes a method for comparing two objects of a given type:

public interface Comparator<T> { public int compare(T o1, T o2); }

The compare method's job is to decide the relative ordering of the two given objects and return an appropriate integer:

<0 0 >0

13

Comparator example

The following Comparator compares Strings, ignoring case: public class CaseInsensitiveComparator implements Comparator<String> { public int compare(String s1, String s2) { return s1.toLowerCase().compareTo( s2.toLowerCase()); } }

14

The sorting methods shown previously can also be called with a Comparator as a second parameter.

The sorting algorithm will use that comparator to order the elements of the array or list.

String[] strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"}; Arrays.sort(strings, new CaseInsensitiveComparator()); System.out.println(Arrays.toString(strings)); [Charlie, DELTA, Foxtrot, alpha, bravo, echo, golf, hotel]

15

Program efficiency

reading: 13.2

16

Efficiency

efficiency: A measure of the computing resources used by a program, such as time, memory, or disk space. time efficiency: How quickly a program runs

5 minutes to render a complex 3D scene for a movie is fast. 5 minutes to search Google is slow.

empirical analysis: Program the algorithm, run it, and time it. algorithm analysis: Applying techniques to mathematically estimate the algorithm's runtime without actually coding it.

17

Most individual statements take 1 unit of time to run. When multiple statements are executed sequentially, their runtimes are added.

When statements are executed in a loop, the runtime is multiplied by the number of repetitions of the loop.

18

More examples

Larger statements can also occur sequentially, in which case their runtimes are added. If larger statements are nested, their runtimes are multiplied.

19

most algorithms' runtime can be expressed as a function of the input size N rate of growth: measure of how quickly the graph of a function rises

we only care about very large input sizes (for small sizes, most any algorithm is fast enough) this helps us discover which algorithms will run more quickly or slowly, for large input sizes

20

Consider these graphs of functions. Perhaps each one represents an algorithm: n3 + 2n2 100n2 + 1000 Which grows faster?

21

How about now?

22

Range algorithm 1

Let's examine the efficiency and growth rate of three algorithms for finding the range (difference between largest and smallest element) in an array. First algorithm (looks at all pairs of values to find which pair is the largest and smallest):

// returns the range of numbers in the given array public static int range(int[] numbers) { int maxDiff = 0; for (int i = 0; i < numbers.length; i++) { for (int j = 0; j < numbers.length; j++) { int diff = Math.abs(numbers[j] - numbers[i]); maxDiff = Math.max(maxDiff, diff); } } return maxDiff; }

Copyright 2006 by Pearson Education

23

Runtime of range 1

24

Range algorithm 2

The first algorithm redundantly makes each comparison twice (when i <= j, when i > j).

public static int range2(int[] numbers) { int maxDiff = 0; for (int i = 0; i < numbers.length; i++) { for (int j = i + 1; j < numbers.length; j++) { int diff = Math.abs(numbers[j] - numbers[i]); maxDiff = Math.max(maxDiff, diff); } } return maxDiff; }

25

Runtime of range 2

26

Range algorithm 3

We can discover the largest and smallest values in a single pass over the array, rather than using many nested passes over all values. Third algorithm:

public static int range3(int[] numbers) { int max = numbers[0]; int min = max; for (int i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; } else if (numbers[i] < min) { min = numbers[i]; } } return max - min; }

Copyright 2006 by Pearson Education

27

Runtime of range 3

Much faster than others Runtime doubles when input size doubles

Copyright 2006 by Pearson Education

28

reading: 13.2

29

Big-Oh notation

Determined by the exponent of the most frequently executed line of code in the algorithm.

Example: If the most frequently executed line of an algorithm runs approximately N3 times, we say the algorithm is "order N3" or O(N3) for short.

We are concerned with how the function grows when N is large. We are not picky about constant factors.

30

Hierarchy of Big-Oh

constant time logarithmic linear loglinear quadratic cubic ... exponential ...

An algorithm from a lower complexity class will run much faster than one from a higher complexity class when the value of N becomes very large.

31

for (int i = 0; i < N; i += c) <statements>;

// O(N)

Adding to the loop counter means that the loop runtime grows linearly when compared to its maximum value N.

Multiplying the loop counter means that the maximum value N must grow exponentially to linearly increase the loop runtime; therefore, it is logarithmic.

Loop executes its body exactly (N2 / c) times.

32

Copyright 2006 by Pearson Education

for (int i = 0; i < N; i += c) { for (int j = 0; j < N; i += c) { <statements>; } } // O(N2)

Loops in sequence add together their runtimes, which means the loop set with the larger runtime dominates.

for (int i = 0; i < N; i += c) { <statements>; } for (int i = 0; i < N; i += c) { for (int j = 0; j < N; i *= c) { <statements>; } }

Copyright 2006 by Pearson Education

// O(N)

// O(N log N)

33

Approximate the value of the variable sum after the following code fragment, as an expression in terms of input size n. Use Big-Oh notation to describe the algorithm's overall runtime.

int sum = 0; for (int i = 1; i <= N; i *= 2) { sum++; } for (int i = 1; i <= 1000; i++) { sum++; sum++; }

34

Approximate the value of the variable sum after the following code fragment, as an expression in terms of input size n. Use Big-Oh notation to describe the algorithm's overall runtime.

int sum = 0; for (int i = 1; i <= N; i++) { for (int j = 1; j <= i / 2; j += 2) { sum++; } }

35

reading: 13.3

36

binary search: An algorithm that searches for a value in a sorted list by repeatedly eliminating half the list from consideration.

Start out with the search area being from indexes 0 to length-1. Examine the element in the middle of the search area.

If it is K, stop. Otherwise,

If it is smaller than K, eliminate the upper half of the search area. If it is larger than K, eliminate the lower half of the search area. Repeat the above examination.

37

searching for value 16

0 1 2 3 4 7 16 20 37 38 43

min

4 5

6

max

38

searching for value 16

0 1 2 3 4 7 16 20 37 38 43

min

max

4 5

6

39

searching for value 16

0 1 2 3 4 7 16 20 37 38 43

4 5

6

40

binary search array a for value i: if all elements have been searched, result is -1. examine middle element a[mid]. if a[mid] equals i, result is mid. if a[mid] is greater than i, binary search lower half of a for i. if a[mid] is less than i, binary search upper half of a for i.

41

// Returns an // appears in // pre: array public static int min = int max = index at which the target the given input array, or -1 if not found. is sorted. int binarySearch(int[] numbers, int target) { 0; numbers.length - 1;

while (min <= max) { int mid = (max + min) / 2; if (numbers[mid] == target) { return mid; // found it! } else if (numbers[mid] < target) { min = mid + 1; // too small } else { // numbers[mid] > target max = mid - 1; // too large } } return -1; }

Copyright 2006 by Pearson Education

// not found

42

How do we analyze the runtime of binary search? The algorithm's runtime is dominated by a while loop. Each pass of that loop cuts the search space in half.

43

reading: 13.3

44

Selection sort

selection sort: orders a list of values by repetitively putting a particular value into its final position more specifically:

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 in their proper places

45

46

Index Value 0 27 1 63 2 1 3 72 4 64 5 58 6 14 7 9

63

27

72

64

58

14

1

1

9

9

27

14

72

72

64

64

58

58

14

27

63

63

47

// places elements of the given array into sorted order public static void selectionSort(int[] a) { for (int i = 0; i < a.length - 1; i++) { // find index of smallest element int smallest = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[smallest]) { smallest = j; } } swap(a, i, smallest); } } public static void swap(int[] list, int i, int j) { int temp = list[i]; list[i] = list[j]; list[j] = temp; }

Copyright 2006 by Pearson Education

48

makes sense because the code performs two nested loops over the array of size N its runtime should be O(N2)

49

[22, 11, 34, -5, 3, 40, 9, 16, 6]

Perform the selection sort algorithm on this data. Write the contents of the array after each pass of the outermost loop of selection sort until the array is sorted.

50

reading: 13.4

51

Merge sort

merge sort: orders a list of values by recursively dividing the list in half until each sub-list has one element, then recombining

Invented by John von Neumann in 1945 relies on observation that if you have two sorted lists of values, they can be combined into a single larger sorted list quickly

split the array into two halves. merge sort the left half (recursively). merge sort the right half (recursively). combine the two halves into a sorted whole.

52

98 23 45 14 98 23 45 14 6 67 33 42 6 67 33 42

98 23

98 23

45 14

45 14

6

6

67

67

33 42

33 42

23 98

14 45

6

6

67

33 42

14 23 45 98 6

33 42 67

14 23 33 42 45 67 98

53

13

0

21

18

20

7

13

0

21

18

3

9

4

20

7

13

0

6

1

21

2

18

3

9

4

4

5

8

6

20

7

13

0

6

1

21

2

18

3

9

4

4

5

8

6

20

7

6

0

13

1

18

2

21

3

4

4

9

5

8

6

20

7

6

0

13

18

21

3

4

4

20

7

4

0

13

18

20

21

7

54

merge operation:

Given two sorted arrays, merge operation produces a sorted array with all the elements of the two arrays

A 6 13 18 21 B 4 8 9 20

13

18

20

21

Running time of merge : O(N), where N is the number of elements in the merged array.

When merging two sorted parts of the same array, we'll need a temporary array to store the merged whole.

55

56

// Returns the first half of the given array. public static int[] leftHalf(int[] array) { int size1 = array.length / 2; int[] left = new int[size1]; for (int i = 0; i < size1; i++) { left[i] = array[i]; } return left; } // Returns the second half of the given array. public static int[] rightHalf(int[] array) { int size1 = array.length / 2; int size2 = array.length - size1; int[] right = new int[size2]; for (int i = 0; i < size2; i++) { right[i] = array[i + size1]; } return right; }

Copyright 2006 by Pearson Education

57

Merge code

// pre : result is empty; left/right are sorted // post: result contains merged sorted lists. public static void merge(int[] result, int[] left, int[] right) { int i1 = 0; // index into left array int i2 = 0; // index into right array for (int i = 0; i < result.length; i++) { if (i2 >= right.length || (i1 < left.length && left[i1] <= right[i2])) { result[i] = left[i1]; // take from left i1++; } else { result[i] = right[i2]; // take from right i2++; } } }

Copyright 2006 by Pearson Education

58

// Places elements of given array into sorted order // using the merge sort algorithm. public static void mergeSort(int[] array) { if (array.length > 1) { // split array into two halves int[] left = leftHalf(array); int[] right = rightHalf(array); // recursively sort the two halves mergeSort(left); mergeSort(right);

// merge the sorted halves into a sorted whole merge(array, left, right);

} }

59

[22, 11, 34, -5, 3, 40, 9, 16, 6]

Perform the merge sort algorithm on this data. Write the contents of the array after each of the recursive calls of merge sort have finished.

60

- Understanding Machine LearningUploaded bydeepakdodo
- DSAUploaded bymishra_leo
- Closest PairUploaded byRichard Siwady Gabrie
- compact-data-structures-practical-approach.epubUploaded byChirinus Alin
- FastDTW PaperUploaded byDavid Polvorosa
- qb2018_EEM303Uploaded byShivam Gautam
- VerizonUploaded bySindhu Selvaraj
- PP Lab ManualUploaded byparasuraman
- DSChapter9.pdfUploaded byprasanna_np
- New Microsoft Word DocumentUploaded byPradeep Raja
- Ch5-Searching-Part1-The Design and Analysis of Parallel Algori - Selim G AklUploaded byHendro Utomo
- Bagshawe's - Proudlock's Egg Bag and Four Race PresentationsUploaded byMarco
- c8-4Uploaded byVinay Gupta
- Apr13certsdreportUploaded byJessica Tiffany
- Internal Sorting MethodsUploaded byKeith Tanaka Magaka
- Convex HullUploaded byশুভ্রা দেবনাথ
- Bubble sort presentationUploaded byMuhammed Ifkaz
- Art of Programming Contest SE for Uva PDFUploaded bygaurav
- Data StructuresUploaded byMaharshi Shukla
- MCA-II Practical List Winter 2014 NewUploaded bySonu Don
- CentroidUploaded byMahir Mahmood
- Algorithms IntroductionUploaded byJack
- AN EFFICIENT APPROACH FOR MINING UNCERTAIN FREQUENT PATTERNS USING DYNAMIC DATA STRUCTURE WITHOUT FALSE POSITIVESUploaded byAnonymous vQrJlEN
- A branch and bound algorithm to minimize the total weighted flowtime for the two-stage assembly scheduling problemUploaded byapi-3862589
- A 1Uploaded byAndrew Olah
- DATA structureUploaded byTED M
- AJOR20110200002_74182109Uploaded byDaniel Alex Tello G
- cs372l03Uploaded byjhdmss
- Average Case 1Uploaded bykrishna kant verma
- Niles A.Pierce and Erik Winfree- Protein Design is NP-hardUploaded byGmso3

- Speech Organs1Uploaded byMarxlen Apostol Bedolido
- Binary Search of SirUploaded byMarxlen Apostol Bedolido
- Research PaperUploaded byMarxlen Apostol Bedolido
- Our ValuesUploaded byMarxlen Apostol Bedolido
- 6 Ways Your Sense of Smell is Secretly Controlling Your MindUploaded byMarxlen Apostol Bedolido
- ComArt1stAssignmentUploaded byMarxlen Apostol Bedolido
- Our StrategyUploaded byMarxlen Apostol Bedolido
- Our HistoryUploaded byMarxlen Apostol Bedolido
- ComArt1stAssignmentUploaded byMarxlen Apostol Bedolido
- Established in 1890 as a SingleUploaded byMarxlen Apostol Bedolido
- cristobalUploaded byMarxlen Apostol Bedolido
- Swapping Code FragmentsUploaded byMarxlen Apostol Bedolido
- Your Sense of SmellUploaded byMarxlen Apostol Bedolido
- When a Person Loses His Sense of Smell, Does He Also Lose Any Memory Associated With a SmellUploaded byMarxlen Apostol Bedolido
- The Organs of SpeechUploaded byMarxlen Apostol Bedolido
- The Eight Parts of Human Speech Organs & Their DefinitionsUploaded byMarxlen Apostol Bedolido
- SpeechUploaded byMarxlen Apostol Bedolido
- Sun Tzu - The Art of War PDFUploaded bycrisgonzal

- Hash Joins - Implementation and TuningUploaded bytebet11
- Circular Linked ListsUploaded byVishnu Prasad Goranthala
- n Jobs and M- Machine ProblemUploaded byMohammed Afshan
- lects1-10Uploaded byRokibul Alam
- CholeSkyUploaded byMuhammad Kaleem
- Disk Scheduling AlgorithmsUploaded byChandresh Kumar Maurya
- Graph Search Methods ExamplesUploaded byapi-3801329
- 1-TimeComplexityUploaded byFensius Musa Aritonang
- fptschool-book.pdfUploaded bysumi_11
- Eee230 Engineering Optimization Using Soft Computing Th 1.00 Ac21Uploaded bySharvanMvs
- CS70 MidtermUploaded byDan Goodman
- Hasle Discrete Optimization - HeuristicsUploaded bymihai37
- cluster analysis.pptUploaded byAmol Rattan
- Chapter 15Uploaded byTalhaAftab
- Sec 83Uploaded byroufix
- SearchingUploaded byrstoikos
- Huffman CodingUploaded byGeetika Bhardwaj
- UNIT 2&3 finalUploaded bykashokcse16
- How to Implement FFT AlgorithmUploaded byPhat Luong
- CS353 10 BacktrackingUploaded byRaedadobeAdobe
- Lab09Uploaded byzakariansu
- Data Structures UVKUploaded byGayathri Suresh
- sha256-384-512Uploaded byMelanie Wallace
- SortingUploaded bymrshahvaizpasha
- PaperUploaded byMitchell Angel Gomez Ortega
- Linear problemUploaded byBhuvana Vignaesh
- StorageStructUploaded bykamalkav
- Binary.treesUploaded byAsafAhmad
- ATPS - Construção de algoritmosUploaded byHermes Vilalba
- 09a1ec01 - c Programming and Data StructuresUploaded byDumpal Koteswararao