Lecture 9-2 : Array Examples : Bubble Sort and Binary Search

Slides:



Advertisements
Similar presentations
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
Advertisements

Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Sorting Algorithms Insertion and Radix Sort. Insertion Sort One by one, each as yet unsorted array element is inserted into its proper place with respect.
Searching Arrays Linear search Binary search small arrays
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
Array operations II manipulating arrays and measuring performance.
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.
EENG212 Algorithms and Data Structures
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.
APS105 Sorting. Sorting is a commonly needed function –itunes can sort your song library different ways –excel spreadsheet can sort a column of numbers.
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
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.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Sorting Dr. Yingwu Zhu. Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order Ascending or descending Some O(n.
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.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Basic Sorting Algorithms Dr. Yingwu Zhu. Sorting Problem Consider list x 1, x 2, x 3, … x n Goal: arrange the elements of the list in order Ascending.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
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 Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Sorting Dr. Yingwu Zhu.
Alternate Version of STARTING OUT WITH C++ 4th Edition
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 9: Searching, Sorting, and Algorithm Analysis
Alg2_1c Extra Material for Alg2_1
Sorting Data are arranged according to their values.
Topic 14 Searching and Simple Sorts
Introduction to Search Algorithms
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
CSC215 Lecture Algorithms.
كلية المجتمع الخرج البرمجة - المستوى الثاني
searching Concept: Linear search Binary search
Sorting Data are arranged according to their values.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Sorting.
HKOI 2005 Intermediate Training
Standard Version of Starting Out with C++, 4th Edition
Sorting.
Sorting.
Sorting Dr. Yingwu Zhu.
Algorithmic Complexity
Topic 14 Searching and Simple Sorts
Topic 24 sorting and searching arrays
Given value and sorted array, find index.
Linear search A linear search sequentially moves through your list looking for a matching value. 1. The element is found, i.e. a[i] = x. 2. The entire.
Searching and Sorting Arrays
COMPUTER 2430 Object Oriented Programming and Data Structures I
Simple Sorting Algorithms
Searching and Sorting Arrays
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Sorting Dr. Yingwu Zhu.
Simple Sorting Algorithms
Sorting Sorting is a fundamental problem in computer science.
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Presentation transcript:

Lecture 9-2 : Array Examples : Bubble Sort and Binary Search Acknowledgment : Lecture notes from Ohio Supercomputing Center

Array arguments in function #include <stdio.h> void myFunction(int yourArray[], int n); int main () { int i; int myArray[4] = {0}; myFunction( myArray, 4); for (i = 0; i < 4; i++) printf(“%d\n”,myArray[i]); return 0; } void myFunction (int yourArray[], int n) for (i = 0; i < n; i++) yourArray[i] = i; 1 2 3

Examples : Sorting , Binary Search

Linear search vs binary search 25 34 7 36 29 91 83 42 6 13 73 54 22 63 12 Search 12 ? In worst case, 15 comparisons are necessary using lineary search sort 6 7 12 13 22 25 29 34 36 42 54 63 73 83 91 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 If sorted, at most 4 comparisons are enough using binary search.

bubble sorting Simple sorting algorithm Relatively slow Time Complexity Worst case O(N^2) Average case O(N^2)

Step-by-step example sorting (5, 1, 4, 2, 8) First Pass : ( 5 1 4 2 8 ) -> ( 1 5 4 2 8 ) ( 1 5 4 2 8 ) -> ( 1 4 5 2 8 ) ( 1 4 5 2 8 ) -> ( 1 4 2 5 8 ) ( 1 4 2 5 8 ) -> ( 1 4 2 5 8 ) Second Pass : ( 1 4 2 5 8 ) -> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) -> ( 1 2 4 5 8 ) Third Pass : ( 1 2 4 5 8 ) -> ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) -> ( 1 2 4 5 8 ) Forth Pass : ( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )

Bubble sort function void bubbleSort(int A[], int N) { int temp,i, j; for (i=N-1;i>=1;i--) for (j=1;j<=i;j++) if (A[j-1]>A[j]) { temp=A[j-1]; A[j-1]=A[j]; A[j]=temp; } #include <stdio.h> void bubbleSort(int A[], int N); int main() { int i; int a[5]={5,1,4,2,8}; bubbleSort(a,5); for (i=0;i<5;i++) printf(“%d\n”,a[i]); return 0; }

Binary Search #include <stdio.h> /* This function searches for v in array a with size N. If found, it returns the array index. If not found, it returns -1. */ int binsearch(int a[], int N, int v) { int l=0; int r=N-1; int x; while (r>=l) { x=(l+r)/2; if (v<a[x]) r=x-1; else l=x+1; if (v==a[x]) return x; } return -1; #include <stdio.h> int binsearch(int a[], int N, int v); int main() { int position; int a[5]={1,2,4,5,8}; position = binsearch(a, 5, 2); printf(“%d\n”,position); return 0; }

binary search (recursive function) /* This function searches for v in array a with size N. If found, it returns the array index. If not found, it returns -1. */ int binsearch(int a[], int N, int v) { // insert your code here }

Bubble Sort example 2 23 17 9 12 15 6 34 1 2 3 4 5 7 8 >? 17 23 9

17 9 12 15 6 23 34 1 2 3 4 5 7 8 >? 17 9 12 15 6 23 34 1 2 3 4 5 7 8  17 9 12 15 6 23 34 1 2 3 4 5 7 8 >? 17 9 12 15 6 23 34 1 2 3 4 5 7 8 17 9 12 15 6 23 34 1 2 3 4 5 7 8 >? 9 17 12 15 6 23 34 1 2 3 4 5 7 8 9 17 12 15 6 23 34 1 2 3 4 5 7 8 >? 9 12 17 15 6 23 34 1 2 3 4 5 7 8 9 12 17 15 6 23 34 1 2 3 4 5 7 8 >? 9 12 15 17 6 23 34 1 2 3 4 5 7 8

9 12 15 17 6 23 34 1 2 3 4 5 7 8 >? 9 12 15 6 17 23 34 1 2 3 4 5 7 8 9 12 15 6 17 23 34 1 2 3 4 5 7 8 >? 9 12 15 6 17 23 34 1 2 3 4 5 7 8 9 12 15 6 17 23 34 1 2 3 4 5 7 8 >? 9 12 15 6 17 23 34 1 2 3 4 5 7 8 9 12 15 6 17 23 34 1 2 3 4 5 7 8 >? 9 12 15 6 17 23 34 1 2 3 4 5 7 8  9 12 15 6 17 23 34 1 2 3 4 5 7 8 >? 9 12 15 6 17 23 34 1 2 3 4 5 7 8 

9 12 15 6 17 23 34 1 2 3 4 5 7 8 >? 9 12 6 15 17 23 34 1 2 3 4 5 7 8 9 12 6 15 17 23 34 1 2 3 4 5 7 8 >? 9 12 6 15 17 23 34 1 2 3 4 5 7 8  9 12 6 15 17 23 34 1 2 3 4 5 7 8 >? 9 12 6 15 17 23 34 1 2 3 4 5 7 8 9 12 6 15 17 23 34 1 2 3 4 5 7 8 >? 9 12 6 15 17 23 34 1 2 3 4 5 7 8  9 12 6 15 17 23 34 1 2 3 4 5 7 8 >? 9 6 12 15 17 23 34 1 2 3 4 5 7 8

9 6 12 15 17 23 34 1 2 3 4 5 7 8 >? 9 6 12 15 17 23 34 1 2 3 4 5 7 8 ≤ 9 6 12 15 17 23 34 1 2 3 4 5 7 8 9 6 12 15 17 23 34 1 2 3 4 5 7 8 >? 6 9 12 15 17 23 34 1 2 3 4 5 7 8 9 6 12 15 17 23 34 1 2 3 4 5 7 8 >? 6 9 12 15 17 23 34 1 2 3 4 5 7 8 ≤ 6 9 12 15 17 23 34 1 2 3 4 5 7 8 >? 6 9 12 15 17 23 34 1 2 3 4 5 7 8 >? 6 9 12 15 17 23 34 1 2 3 4 5 7 8 ≤

6 9 12 15 17 23 34 1 2 3 4 5 7 8 >? 6 9 12 15 17 23 34 1 2 3 4 5 7 8 ≤ 6 9 12 15 17 23 34 1 2 3 4 5 7 8 >? 6 9 12 15 17 23 34 1 2 3 4 5 7 8 ≤ 6 9 12 15 17 23 34 1 2 3 4 5 7 8 >? 6 9 12 15 17 23 34 1 2 3 4 5 7 8 ≤ 6 9 12 15 17 23 34 1 2 3 4 5 7 8 Sorted!