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.

Slides:



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

Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
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.
Data Structures and Algorithms
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.
CHAPTER 11 Sorting.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 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.
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
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.
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.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Computer Science Searching & Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
February 4, 2005 Searching and Sorting Arrays. Searching.
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.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Data Structure Introduction.
Sort Algorithms.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
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
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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.
©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.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
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 Searching algorithms with simple arrays
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
Searching and Sorting Algorithms
Sorting Why? Displaying in order Faster Searching Categories Internal
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Teach A level Computing: Algorithms and Data Structures
Topic 14 Searching and Simple Sorts
Introduction to Search Algorithms
Unit IV : Searching and sorting Techniques
CSC215 Lecture Algorithms.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Search,Sort,Recursion.
slides adapted from Marty Stepp
Topic 14 Searching and Simple Sorts
Searching.
Search,Sort,Recursion.
CSE 373 Data Structures and Algorithms
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Presentation transcript:

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 ’ s memory (or sometimes on a disk). Data structures include linked lists, arry, stacks, binary trees, and hash tables. Algorithms is manipulate the data in these structures in various ways, such as inserting a new data item, searching for a particular item, or sorting the items.

Type of Sorting Type of Sorting Insertio n sort Selection sort Bubble sort Quick sort Merge Sort

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.

void insertionSort(int numbers[], int array_size) { int i, j, index; for (i=1; i < array_size; i++) {index = numbers[i]; j = i; while ((j > 0) && (numbers[j-1] > index)) {numbers[j] = numbers[j-1]; j = j - 1; } numbers[j] = index;} Inner loop outer loop

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)

void SelectionSort(int A[], int length) { int i, j, min, minat; for(i = 0; i<(length-1); i++) { minat = i; min = A[i]; for(j = i+1;j < length; j++) //select the min of the rest of array { if(min > A[j]) //ascending order for descending reverse { minat = j; //the position of the min element min = A[j]; } int temp = A[i]; A[i] = A[minat]; //swap A[minat]=temp; } }//end selection sort Code for swap Code for select the min

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

void bubble(int a[ ],int n) { int i,t; for(i=n-2;i>=0;i--) { for(j=0;j<=i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } }//end for 1. }//end function. Code for swap

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#6

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;} Code for search

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; }

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.