1 ICS103 Programming in C Lecture 14: Searching and Sorting.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

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.
CSE Lecture 3 – Algorithms I
ICS103: Programming in C Searching, Sorting, 2D Arrays
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
HST 952 Computing for Biomedical Scientists Lecture 9.
Searching and Sorting Linear Search Binary Search Selection Sort
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
1 Lecture 23 Searching Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
1 Lecture 21 Introduction to Sorting I Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.  Brief.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
Unit 271 Searching and Sorting Linear Search Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 Sorting Algorithms (Part I) Sorting Algoritms (Part I) Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.
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.
Chapter 8 ARRAYS Continued
1 ICS103 Programming in C Ch7: Arrays. O BJECTIVES What is an Array? Declaring Arrays Visual representation of an Array Array Initialization Array Subscripts.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
UNIT 18 Searching and Sorting.
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.
Chapter 19: Searching and Sorting Algorithms
Computer Science Searching & Sorting.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
1 ICS103 Programming in C Lecture 14: Searching and Sorting.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
ICS103: Programming in C Searching, Sorting, 2D Arrays Muhamed F. Mudawar.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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
ICS103 Programming in C Lecture 14: Searching and Sorting
Searching and Sorting Algorithms
Sorting Mr. Jacobs.
Lecture 14 Searching and Sorting Richard Gesick.
CSc 110, Spring 2017 Lecture 39: searching.
CSC215 Lecture Algorithms.
ICS103 Programming in C Lecture 13: Arrays II
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Search,Sort,Recursion.
Search,Sort,Recursion.
Searching and Sorting Arrays
UNIT – IV Searching – Linear search - binary search Sorting
Module 8 – Searching & Sorting Algorithms
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

1 ICS103 Programming in C Lecture 14: Searching and Sorting

2 Outline Searching  Linear Search Algorithm  Linear Search Implementation  Binary Search Algorithm  Binary Search Implementation Sorting  Selection Sort Algorithm  Selection Sort Implementation  Bubble Sort Algorithm  Bubble Sort Implementation

3 Introduction to Searching Searching means scanning through a list of items (in an array) to find if a particular one exists. It usually requires the user to specify the target item – the item he wishes to locate If the target item is found, the item or its location (index) is returned, otherwise, an appropriate message or flag is returned. An important issue in processing a search request is response time. Some factors affecting response time are:  The size of the array to search from  The organization of data in the array; random or ordered  The searching method or algorithm; linear or binary In this lecture, we study two searching methods; Linear Search and Binary Search.

4 Linear Search Algorithm This involves searching through the array sequentially until the target item is found or the array is exhausted. If the target is found, its location is returned, otherwise a flag such as –1 is returned. Here is the algorithm for Linear Search: 1.Assume that the target has not been found 2.Start with initial array element 3.Repeat while the target is not found and there are more array elements: 4.If the current element matches the target 5.Set a flag to indicate that the target has been found else 6.Advance to the next array element 7.If the target was found 8.Return the target index as the search result else 9.Return -1 as the search result

5 Linear Search Implementation #include #define SIZE 8 int linear_search(double a[], double target, int size); void read_array(double a[], int size); int main(void) { double x[SIZE], target; int index; read_array(x, SIZE); printf("Enter Element to search for: "); scanf("%lf", &target); index = linear_search(x, target, SIZE); if (index != -1) printf("Target was found at index %d\n", index); else printf("Sorry, target item was not found"); system("pause"); return 0; } void read_array (double a[], int size) { int i; printf("Enter %d integer numbers separated by blanks\n> ", size); for (i = 0; i < size; ++i) scanf("%lf", &a[i]); } /* Searches for target in an array using Linear search; * Returns index of target or -1 if not found */ int linear_search(double a[], double target, int size) { int i, ; i = 0; while (!found && i < size) { if (a[i] == target) return 1; } return -1; }

6 Binary Search Algorithm  For a list of n elements, the linear search takes an average of n/2 comparisons to find an item, with the best case being 1 comparison and the worst case being n comparisons.  However, if the list is ordered, it is a waste of time to look for an item using linear search - it would be like looking for a word in a dictionary sequentially.  In this case we apply a more efficient method called binary search. Binary search works by comparing the target with the item at the middle of the list. This leads to one of three results: 1. If the middle item is the target – we are done. 2. If the middle item is less than target, we apply the algorithm to the upper half of the list. 3.If the middle item is bigger than the target, we apply the algorithm to the lower half of the list.  This process is repeated until the item is found or the list is exhausted

7 Binary Search Algorithm ….

8 Binary Search Implementation #include #define SIZE 8 int binary_search (double x[], int low, int high, double target); void read_array(double a[], int size); int main(void) { double x[SIZE], target; int index; read_array(x, SIZE); printf("Enter Element to search for: "); scanf("%lf", &target); index = binary_search(x, 0, SIZE-1, target); if (index != -1) printf("Target was found at index %d\n", index); else printf("Sorry, target item was not found"); system("pause"); return 0; } void read_array (double a[], int size) { int i; printf("Enter %d integer numbers separated by blanks\n> ", size); for (i = 0; i < size; ++i) scanf("%lf", &a[i]); } /* Recursive implementation of binary search */ int binary_search (double x[], int low, int high, double target) { int middle; if (low > high) /*base case1:target not found*/ return -1; middle = (low + high)/2; if (x[middle] == target) return (middle); /*base case2:target found*/ else if (x[middle] < target) return binary_search(x, middle+1,high,target); else return binary_search(x, low, middle-1,target); }

9 Introduction to Sorting Sorting is the re-arrangement of a collection of data in increasing or decreasing oreder according to some key-field. It is a common activity in data management. Even when a list is maintained in a certain order, there is often a need to re-arrange the list in a different order. Because it takes so much processing time, sorting is a serious topic in computer science, and many different sorting algorithms have been designed. We shall consider two of such sorting methods; Selection sort and Bubble Sort.

10 Selection Sort Algorithm  Selection sort involved scanning through the list to find (or select) the smallest element and swap it with the first element.  The rest of the list is then search for the next smallest and swap it with the second element.  This process is repeated until the rest of the list reduces to one element, by which time the list is sorted.  The following table shows how selection sort works.

11 Selection Sort Implementation #include #define SIZE 10 void selection_sort(double a[], int size); void read_array(double a[], int size); void print_array(double a[], int size); int find_min(double a[], int start, int size); void swap(double *a, double *b); int main(void) { double x[SIZE]; int i; read_array(x, SIZE); printf("Before Sorting: "); print_array(x, SIZE); selection_sort(x, SIZE); printf("After Sorting: "); print_array(x, SIZE); system("pause"); return 0; } void selection_sort(double a[], int size) { int i, min_pos; for (i = 0; i<=size-2; i++) { min_pos = find_min(a, i, size); swap(&a[i], &a[min_pos]); } } int find_min(double a[], int start, int size) { int i, min_index = start; for (i=start+1; i<size; i++) if (a[i] < a[min_index]) min_index = i; return min_index; } void swap(double *a, double *b) { double temp = *a; *a = *b; *b = temp; } void read_array (double a[], int size) { int i; printf("Enter %d integer numbers separated by blanks\n> ", size); for (i = 0; i < size; ++i) scanf("%lf", &a[i]); } void print_array(double a[], int size) { int i; for (i = 0; i < size; ++i) printf("%.1f ", a[i]); printf("\n"); }

12 Bubble Sort Algorithm  The idea of Bubble (or exchange) sort is to scan through the list and swap each pair of adjacent elements that are in the wrong order.  The process is repeated each time from index zero to one less than the previous limit until either the list is exhausted or until a pass that involve no swap is encountered.  At the end of first pass, the largest element will move (or bubble up) to the end of the list.  At the end of the second swap, the second largest will move to its right place, etc.  The following table shows a trace of how bubble sort works.

13 Bubble Sort Implementation #include #define SIZE 10 void bubble_sort(double a[], int size); void read_array(double a[], int size); void print_array(double a[], int size); void swap(double *a, double *b); int main(void) { double x[SIZE]; int i; read_array(x, SIZE); printf("Before Sorting: "); print_array(x, SIZE); bubble_sort(x, SIZE); printf("After Sorting: "); print_array(x, SIZE); system("pause"); return 0; } void swap(double *a, double *b) { double temp = *a; *a = *b; *b = temp; } void bubble_sort(double a[], int size) { int i, pass = 1, swap_occurs; do{ swap_occurs = 0; for(i = 1; i <= size - pass; i++) { if (a[i - 1] > a[i]) { swap(&a[i-1], &a[i]); swap_occurs = 1; } } pass++; } while (swap_occurs && pass <= size-1); } void read_array (double a[], int size) { int i; printf("Enter %d integer numbers separated by blanks\n> ", size); for (i = 0; i < size; ++i) scanf("%lf", &a[i]); } void print_array(double a[], int size) { int i; for (i = 0; i < size; ++i) printf("%.1f ", a[i]); printf("\n"); }