LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.

Slides:



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

CSE Lecture 3 – Algorithms I
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.
Computation for Physics 計算物理概論 Algorithm. An algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing,
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
CHAPTER 11 Sorting.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
 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
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
February 4, 2005 Searching and Sorting Arrays. Searching.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
CSC 211 Data Structures Lecture 13
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Sort Algorithms.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
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.
3 – SIMPLE SORTING ALGORITHMS
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.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
©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.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Chapter 16: Searching, Sorting, and the vector Type.
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 Searching algorithms with simple arrays
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
Chapter 16: Searching, Sorting, and the vector Type
Searching and Sorting Algorithms
Lecture No.45 Data Structures Dr. Sohail Aslam.
Sorting Why? Displaying in order Faster Searching Categories Internal
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
CSC215 Lecture Algorithms.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Search,Sort,Recursion.
Topic 24 sorting and searching arrays
Searching.
Search,Sort,Recursion.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

LAB#7

Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts at out and moves left, until either temp is smaller than the array element there, or it can’t go left any further. Each pass through the while loop shifts another sorted element one space right.

Example #1: Show the steps of sorting the following array:

Example #2: Show the steps of sorting the following array:

Selection Sort : 1.Find the minimum value in the list 2.Swap it with the value in the first position 3.Repeat the steps above for the remainder of the list (starting at the second position and advancing each time)

Example #1: Show the steps of sorting the following array:

Example #2: Show the steps of sorting the following array:

Bubble Sort : 1.compares the first two elements 2.If the first is greater than the second, swaps them 3.continues doing this for each pair of elements 4.Starts again with the first two elements, repeating until no swaps have occurred on the last pass

Example #1: Show the steps of sorting the following array:

Example #2: Show the steps of sorting the following array:

Quick Sort : Quick sort is a divide and conquer algorithm. Quick sort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quick sort can then recursively sort the sub-lists. A Quick sort works as follows: 1.Choose a pivot value: We take the value of the middle element, but it can be any value. 2.Partition. 3. Sort both part: Apply quick sort algorithm recursively to the left and the right parts.

Example #1: Show the steps of sorting the following array:

Example #2: Show the steps of sorting the following array:

Merge Sort : Merge sort is a much more efficient sorting technique than the bubble Sort and the insertion Sort at least in terms of speed. A merge sort works as follows: 1.Divide the unsorted list into two sub lists of about half the size. Sort each sub list recursively by re-applying the merge sort. 2.Merge the two sub lists back into one sorted list.

Example #1: Show the steps of sorting the following array:

Example #2: Show the steps of sorting the following array:

LAB#7

Linear Search : Linear Search : Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list.

Linear Search : ?=12

#include using namespace std; int LinearSearch(int Array[],int Size,int ValToSearch) { bool NotFound = true; int i = 0; while(i < Size && NotFound) {if(ValToSearch != Array[i]) i++; else NotFound = false; } if( NotFound == false ) return i; else return -1;}

int main(){ int Number[] = { 67, 278, 463, 2, 4683, 812, 236, 38 }; int Quantity = 8; int NumberToSearch = 0; cout << "Enter the number to search: "; cin >> NumberToSearch; int i = LinearSearch(Number, Quantity, NumberToSearch); if(i == -1) cout << NumberToSearch << " was not found in the collection\n\n"; else { cout << NumberToSearch << " is at the index " << i<<endl; return 0; }

Binary Search : Binary Search >>> sorted array. Binary Search Algorithm : 1.get the middle element. 2.If the middle element equals to the searched value, the algorithm stops. 3.Otherwise, two cases are possible: o searched value is less, than the middle element. Go to the step 1 for the part of the array, before middle element. o searched value is greater, than the middle element. Go to the step 1 for the part of the array, after middle element.

Example Binary Search :

#include using namespace std; int binarySearch(int arr[], int value, int left, int right) { while (left <= right) { int middle = (left + right) / 2; // compute mid point. if (arr[middle] == value)// found it. return position return middle; else if (arr[middle] > value) // repeat search in bottom half. right = middle - 1; else left = middle + 1; // repeat search in top half. } return -1; }

void main() { int x=0; int myarray[10]={2,5,8,10,20,22,26,80,123,131}; cout<<"Enter a searched value : "; cin>>x; if(binarySearch(myarray,x,0,9)!=-1) cout<<"The searched value found at position : "<<binarySearch(myarray,x,0,9)<<endl; else cout<<"Not found"<<endl; }

void main() { int x=0; int myarray[10]={2,5,8,10,20,22,26,80,123,131}; cout<<"Enter a searched value : "; cin>>x; if(binarySearch(myarray,x,0,9)!=-1) cout<<"The searched value found at position : "<<binarySearch(myarray,x,0,9)<<endl; else cout<<"Not found"<<endl; }

Exercise #1 : Write a C++ program that define an array myarray of type integer with the elements (10,30,5,1,90,14,50,2). Then the user can enter any number and found if it is in the array or not. A.Use linear search. B.Edit the previous program and use binary search.