CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Similar documents
CSE202- Lec#6. (Operations on CSE202 C++ Programming

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Lecture 6 Sorting and Searching

C/C++ Programming Lecture 18 Name:

Sorting Algorithms. Array Data is being arranged in ascending order using the bubble sort algorithm. #1 #2 #3 #4 #5 #6 #7

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Arrays. Week 4. Assylbek Jumagaliyev

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Arrays and Applications

Chapter 10 - Notes Applications of Arrays

LECTURE 17. Array Searching and Sorting

C Arrays Pearson Education, Inc. All rights reserved.

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

Computer Programming Lecture 14 Arrays (Part 2)

Sorting & Searching. Hours: 10. Marks: 16

An array is a collection of similar elements that is all the elements in an array should have same data type. This means that an array can store

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Prepared By:- Dinesh Sharma Asstt. Professor, CSE & IT Deptt. ITM Gurgaon

To become familiar with array manipulation, searching, and sorting.

EENG 212 Lab 2. Recursive Functions

Downloaded from

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Subject: Fundamental of Computer Programming 2068

8.1. Chapter 8: Introduction to Search Algorithms. Linear Search. Linear Search. Linear Search - Example 8/23/2014. Introduction to Search Algorithms

Search,Sort,Recursion

Searching, Sorting. Arizona State University 1

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

CS 106 Introduction to Computer Science I

Searching & Sorting in Java Bubble Sort

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

Chapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

LECTURE 08 SEARCHING AND SORTING ARRAYS

Algorithm Analysis. Performance Factors

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Sorting/Searching and File I/O. Sorting Searching Reading for this lecture: L&L

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

4. SEARCHING AND SORTING LINEAR SEARCH


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Sorting Algorithms. Selection Sort Algorithm

Sorting Pearson Education, Inc. All rights reserved.

Questions Bank. 14) State any four advantages of using flow-chart

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Computers in Engineering COMP 208. Where s Waldo? Linear Search. Searching and Sorting Michael A. Hawker

Solutions to Chapter 8

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Data Structures. Chapter 04. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

Functions. Arash Rafiey. September 26, 2017

Searching. 11. Searching

Here we will only consider the problem of searching among the elements of an array. Intro Programming in C++

Initialisation of an array is the process of assigning initial values. Typically declaration and initialisation are combined.

Lecture 04 FUNCTIONS AND ARRAYS

C++ PROGRAMMING SKILLS Part 4: Arrays

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

International Journal Of Engineering Research & Management Technology

4.2 Sorting and Searching. Section 4.2

Introduction to Computer Science Midterm 3 Fall, Points

Chapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning.

Q (Quaternary) Search Algorithm

DATA STRUCTURES/UNIT 3

Computer Science & Engineering 150A Problem Solving Using Computers

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Principles of Programming. Chapter 6: Arrays

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Chapter 8 Search and Sort

Cpt S 122 Data Structures. Sorting

Decision Making -Branching. Class Incharge: S. Sasirekha

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Decision Making and Loops

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

SCJ2013 Data Structure & Algorithms. Bubble Sort. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

a) State the need of data structure. Write the operations performed using data structures.

CSCI 2132 Software Development Lecture 18: Implementation of Recursive Algorithms

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

UNIT 7. SEARCH, SORT AND MERGE

Data Structures and Algorithms(8)

Sorting Algorithms. + Analysis of the Sorting Algorithms

Chapter 10 Sorting and Searching Algorithms

16. Searching and Sorting

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

Outline. What s wrong with Enumerations? Indexing Searching Self-Organizing Vectors Binary Searching Sorting Command line Arguments.

Searching & Sorting in Java Shell Sort

Binary Search and Worst-Case Analysis

Binary Search and Worst-Case Analysis

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

Prepared by: Shraddha Modi

Sorting and Searching

Sorting and Searching -- Introduction

LECTURE NOTES ON DATA STRUCTURES THROUGH C

Chapter Objectives. List Processing. Chapter 10: Applications of Arrays and Strings. Java Programming: Program Design Including Data Structures

DECISION MAKING STATEMENTS

Transcription:

CSE101-lec#19 Array searching and sorting techniques Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Outline Introduction Linear search Binary search Bubble sort

Introduction The process of finding a particular element of an array is called searching. Search an array for a key value. Two searching techniques: Linear search Binary search

Linear search Linear search Simple Compare each element of array with key value Useful for small and unsorted arrays It simply examines each element sequentially, starting with the first element, until it finds the key element or it reaches the end of the array. Example: If you were looking for someone on a moving passenger train, you would use a sequential search.

#include <stdio.h> void main() int a[10]= 2,6,4,8,10,12,89,68,45,37}; int i, loc = -1, key; printf("enter integer value to search in array:"); scanf( "%d", &key ); // attempt to locate searchkey in array a for ( i = 0; i < 10; i++ ) if ( array[i] == key ) loc = i+1; // location of key is stored break; } // end if } // end for Program of linear search in an array. if(loc!= -1) printf( Element found at %d",loc+1); } else printf( Element not found"); } } // end main

Enter integer to search in array: 4 Element found at 3 output Enter integer search in array: 85 Element not found

Binary search Binary search Applicable for sorted arrays The algorithm locates the middle element of the array and compares it to the key value. Compares middle element with the key If equal, match found If key < middle, looks in left half of middle If key > middle, looks in right half of middle Repeat (the algorithm is repeated on one-quarter of the original array.)

Binary search It repeatedly divides the sequence in two, each time restricting the search to the half that would contain the element. This is a tremendous increase in performance over the linear search that required comparing the search key to an average of half of the array elements. You might use the binary search to look up a word in a dictionary

#include<stdio.h> int main() int loc=-1, key, beg=0,last=9,mid; int a[10]= 2,4,6,8,10,12,37,45,68,89};//sorted array printf("enter integer value to search in sorted array:"); scanf( "%d", &key ); while(beg<=last) mid = (beg + last) / 2; // determine middle element Program of binary search in an array } if(a[mid]==key) loc=mid; //save the location of element. break; } else if(a[mid]>key) //key less than middle element last=mid-1; } else if(a[mid]<key) //key greater than middle element beg=mid+1; } //end of if else } //end of while if(loc!=-1) printf("element found at %d", loc+1); } else printf("element not found"); } getch();

Enter integer to search in sorted array: 4 Element found at 2 output Enter integer search in sorted array: 85 Element not found

Sorting Sorting data Important computing application Virtually every organization must sort some data

Bubble sort Bubble sort (sinking sort) A simple but inefficient sorting technique. Several passes through the array Successive pairs of elements are compared If increasing order (or identical ), no change If decreasing order, elements exchanged Repeat

Bubble sort Comparing successive elements 77 56 4 10 34 2 56 4 10 34 2 77 4 10 34 2 56 77 4 10 2 34 56 77 4 2 10 34 56 77 2 4 10 34 56 77 Original array After pass 1 After pass 2 After pass 3 After pass 4 After pass 5 Total number of pass required for sorting: n-1

Bubble sort It s called bubble sort or sinking sort because smaller values gradually bubble their way to the top of the array (i.e., toward the first element) like air bubbles rising in water, while the larger values sink to the bottom (end) of the array. The technique uses nested loops to make several passes through the array. Each pass compares successive pairs of elements. If a pair is in increasing order (or the values are equal), the bubble sort leaves the values as they are. If a pair is in decreasing order, the bubble sort swaps their values in the array.

Bubble sort The first pass compares the first two elements of the array and swaps their values if necessary. It then compares the second and third elements in the array. The end of this pass compares the last two elements in the array and swaps them if necessary. After one pass, the largest element will be in the last index. After two passes, the largest two elements will be in the last two indices.

#include <stdio.h> int main() int a[10] = 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; int pass, hold; // temporary location used to swap array elements printf( "Data items in original order" ); for ( i = 0; i < 10; ++i ) printf( "%4d", a[ i ] ); } // end for // bubble sort // loop to control number of passes for ( pass = 1; pass < 10; ++pass ) // loop to control number of comparisons per pass for ( i = 0; i < 10-1; ++i ) // compare adjacent elements and swap them if first // element is greater than second element if ( a[ i ] > a[ i + 1 ] ) hold = a[ i ]; a[ i ] = a[ i + 1 ]; a[ i + 1 ] = hold; } // end if } // end inner for } // end outer for This program demonstrates the working of bubble sort printf( "\ndata items in ascending order" ); for ( i = 0; i < 10; ++i ) printf( "%4d", a[ i ] ); } // end for } // end main

Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89 output

Next Class: Defining and initializing strings cse101@lpu.co.in