Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search.

Slides:



Advertisements
Similar presentations
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Advertisements

Selection Sort. Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting.
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
CSE 373: Data Structures and Algorithms
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
C++ Plus Data Structures
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
1 Arrays 2: Sorting and Searching Admin. §1) No class Thursday. §2) Will cover Strings next Tuesday. §3) Take in report. §4) Hand out program assignment.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
CSE 373 Data Structures and Algorithms
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
קורס מחשב לרפואנים הרצאה 8: סיבוכיות ומיון ראובן בר-יהודה. © כל הזכויות שמורות לטכניון – מכון טכנולוגי לישראל.
Analysis of Bubble Sort and Loop Invariant
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Lecture -3 on Data structures Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Array Data structures are classified as either linear or nonlinear.
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
CSCI 51 Introduction to Programming March 12, 2009.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Bubble Sort.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Bubble Sort Example
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Sorting Sorting takes an unordered array and makes it an ordered one
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
Insertion Sort while some elements unsorted: Using linear search, find the location in the sorted portion where the 1 st element of the unsorted portion.
CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CSCI 51 Introduction to Programming March 10, 2009.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching and Sorting Searching algorithms with simple arrays
The Bubble Sort Mr. Dave Clausen La Cañada High School
CS212: Data Structures and Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Sorting.
Recitation 13 Searching and Sorting.
Sorting Algorithms.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Linear and Binary Search
Sorting Algorithms IT12112 Lecture 07.
Analysis of Bubble Sort and Loop Invariant
Lecture 11 Searching and Sorting Richard Gesick.
Lecture 16 Bubble Sort Merge Sort.
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Visit for More Learning Resources
Sorting Example Bubble Sort
Module 8 – Searching & Sorting Algorithms
Visit for more Learning Resources
Presentation transcript:

Arrays (Continue) Sorting and searching

outlines Sorting – Bubble sort Linear search – min and max Binary search

Sort Bubble Sort

Sorting Sorting takes an unordered collection and makes it an ordered one

"Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end – Bubble the largest value to the end using pair- wise comparisons and swapping Swap 4277

"Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end – Bubble the largest value to the end using pair- wise comparisons and swapping Swap 3577

"Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end – Bubble the largest value to the end using pair- wise comparisons and swapping Swap 1277

"Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end – Bubble the largest value to the end using pair- wise comparisons and swapping No need to swap

"Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end – Bubble the largest value to the end using pair- wise comparisons and swapping Swap 5101

"Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end – Bubble the largest value to the end using pair- wise comparisons and swapping Largest value correctly placed

The Bubble Up Algorithm for(int i = 0; i<A.length; i++) { for(int j = 0; j < A.length-1; j++) { if(A[j]>A[j+1]) { temp= A[j]; A[j]=A[j+1]; A[j+1]=temp; }

Items of Interest Notice that only the largest value is correctly placed All other values are still out of order So we need to repeat this process Largest value correctly placed

Repeat Bubble Up How Many Times? If we have N elements… And if each time we bubble an element, we place it in its correct location… Then we repeat the bubble up process N – 1 times. This guarantees well correctly place all N elements.

Bubbling All the Elements N - 1

Reducing the Number of Comparisons

for(int i = 0; i<A.length; i++) { for(int j = 0; j < A.length-1; j++) { if(A[j]>A[j+1]) { temp= A[j]; A[j]=A[j+1]; A[j+1]=temp; }

Already Sorted Collections? What if the collection was already sorted? What if only a few elements were out of place and after a couple of bubble ups, the collection was sorted? We want to be able to detect this and stop early!

Using a Boolean Flag We can use a boolean variable to determine if any swapping occurred during the bubble up. If no swapping occurred, then we know that the collection is already sorted! This boolean flag needs to be reset after each bubble up.

Add flag to reduce the number of iterations Boolean swap = false; for(int i = 0; i<A.length && !swap; i++) { boolean swap = false; for(int j = 0; j < A.length-1; j++) { if(A[j]>A[j+1]) { temp= A[j]; A[j]=A[j+1]; A[j+1]=temp; swap = true; }

An Animated Example j index 0 A.length 8 swap true

An Animated Example to_do index 0 1 A.lenght 8 did_swap false

An Animated Example to_do index 0 1 N 8 Swap did_swap false

An Animated Example to_do index 0 1 N 8 Swap did_swap true

An Animated Example j index 7 2 A.length 8 did_swap true

An Animated Example to_do index 7 2 N 8 Swap did_swap true

An Animated Example j index 0 2 A.length 8 Swap did_swap true

An Animated Example j index 0 3 A.length 8 swap true

An Animated Example j index 0 3 A.lengt 8 Swap swap true

An Animated Example j index 0 3 A.lengt 8 Swap swap true

An Animated Example j index 0 4 A.lengt 8 swap true

An Animated Example j index 0 4 A.length 8 Swap did_swap true

An Animated Example j index 0 4 A.length 8 Swap did_swap true

An Animated Example j index 0 5 A.lengt 8 swap true

An Animated Example j index 0 5 A.lengt 8 Swap swap true

An Animated Example j index 0 5 A.length 8 Swap did_swap true

An Animated Example j index 0 6 A.length 8 swap true

An Animated Example j index 0 6 A.lengt 8 Swap swap true

An Animated Example j index 0 6 A.lengt 8 Swap swap true

An Animated Example j index 0 7 A.lengt 8 swap true

An Animated Example j index 0 7 A.length 8 Swap swap true

An Animated Example j index 0 7 A.length 8 Swap swap true

After First Pass of Outer Loop j index 0 8 A.length 8 Finished first Bubble Up swap true

The Second Bubble Up j index 2 1 A.length 8 swap false

The Second Bubble Up j index 2 1 A.length 8 swap false No Swap

The Second Bubble Up j index 1 2 A.length 8 swap false

The Second Bubble Up j index 1 2 A.length 8 did_swap false Swap

The Second Bubble Up j index 1 2 A.length 8 did_swap true Swap

The Second Bubble Up j index 1 3 A.length 8 swap true

The Second Bubble Up j index 1 3 A.length 8 did_swap true Swap

The Second Bubble Up j index 1 3 A.length 8 swap true Swap

The Second Bubble Up j index 1 4 A.length 8 did_swap true

The Second Bubble Up to_do index 1 4 N 8 swap true No Swap

The Second Bubble Up to_do index 1 5 N 8 swap true

The Second Bubble Up to_do index 1 5 N 8 swap true Swap

The Second Bubble Up j index 1 5 A.length 8 swap true Swap

The Second Bubble Up j index 1 6 A.length 8 swap true

The Second Bubble Up to_do index 1 6 N 8 swap true Swap

The Second Bubble Up to_do index 1 6 N 8 swap true Swap

After Second Pass of Outer Loop index 1 7 N 8 swap true Finished second Bubble Up

The Third Bubble Up j index 2 1 A.length 8 swap false

The Third Bubble Up j index 2 1 A.length 8 swap false Swap

The Third Bubble Up j index 2 1 A.length 8 swap true Swap

The Third Bubble Up j index 2 2 A.length 8 swap true

The Third Bubble Up j index 2 2 A.lengt 8 swap true Swap

The Third Bubble Up j index 2 2 A.lengt 8 swap true Swap

The Third Bubble Up to_do index 2 3 N 8 did_swap true

The Third Bubble Up j index 2 3 A.length 8 swap true No Swap

The Third Bubble Up j index 2 4 A.length 8 did_swap true

The Third Bubble Up j index 2 4 A.length 8 swap true Swap

The Third Bubble Up j index 2 4 A.length 8 swap true Swap

The Third Bubble Up j index 2 5 A.length 8 did_swap true

The Third Bubble Up j index 2 5 A.length 8 swap true Swap

The Third Bubble Up j index 2 5 A.length 8 did_swap true Swap

After Third Pass of Outer Loop j index 2 6 A.length 8 did_swap true Finished third Bubble Up

The Fourth Bubble Up j index 3 1 A.length 8 swap false

The Fourth Bubble Up j index 3 1 A.length 8 swap false Swap

The Fourth Bubble Up to_do index 3 1 N 8 did_swap true Swap

The Fourth Bubble Up j index 3 2 A.length 8 swap true

The Fourth Bubble Up j index 3 2 A.length 8 did_swap true No Swap

The Fourth Bubble Up j index 3 3 A.length 8 swap true

The Fourth Bubble Up j index 3 3 A.length 8 did_swap true No Swap

The Fourth Bubble Up j index 3 4 A.length 8 swap true

The Fourth Bubble Up j index 3 4 A.length 8 did_swap true No Swap

After Fourth Pass of Outer Loop j index 3 5 A.length 8 swap true Finished fourth Bubble Up

The Fifth Bubble Up j index 4 1 A.length 8 swap false

The Fifth Bubble Up j index 4 1 A.lenght 8 swap false No Swap

The Fifth Bubble Up j index 4 2 A.length 8 swap false

The Fifth Bubble Up j index 4 2 A.length 8 swap false No Swap

The Fifth Bubble Up j index 4 3 A.length 8 swap false

Finished Early j index 3 4 A.lengt 8 swap false We didnt do any swapping, so all of the other elements must be correctly placed. We can skip the last two passes of the outer loop.

Bubble sort Summary Bubble Up algorithm will move largest value to its correct location (to the right) Repeat Bubble Up until all elements are correctly placed: – Maximum of N-1 times – Can finish early if no swapping occurs We reduce the number of elements we compare each time one is correctly placed

Linear search Use linear to seach and value is contained in one array.

Searching for x in an array a for(int i = 0; i < a.length; ++ i) { if (a[i] == x) System.out.println(true); }

Finding the minimal value in array int minValue = a[0]; int minIndex = 0; for(int i = start + 1; i < end; i ++) { if (a[i] < minValue) { minValue = a[i]; minIndex = i; }

Finding the minimal value in array int maxValue = a[0]; int maxIndex = 0; for(int i = start + 1; i < end; i ++) { if (a[i] > maxValue) { maxValue = a[i]; maxIndex = i; }

Binary search for a value in array Searching if x is in array a. We assume a is sorted in ascending order. int start = 0; int end = a.length - 1; boulean found = flase while (start <= end) { int m = (start + end)/2; if (x == a[m]) {found =false;} else if (x < a[m]) { end = m - 1;} else { start = m + 1; }

summary Buble sort Learnear search Binary sort