EENG212 Algorithms and Data Structures

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Garfield AP Computer Science
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Dr. Sajib Datta  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
Arrays Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
1 ICS103 Programming in C Lecture 14: Searching and Sorting.
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
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
Data Structure Algorithm Analysis TA: Abbas Sarraf
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
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)
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
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.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
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.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
C Lecture Notes 1 Arrays (...cont.). C Lecture Notes 2 6.6Sorting Arrays Sorting data –Important computing application –Virtually every organization must.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
1.1 Data Structure and Algorithm Lecture 1 Array Record Sequential Search Binary Search Bubble Sort Recursion Complexity Topics.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Data Structure and Algorithms
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Arrays Case Study.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching Arrays Linear search Binary search small arrays
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
Module 5 Sorting and Searching: Bubble sort Selection sort
Introduction to Search Algorithms
CSC215 Lecture Algorithms.
Searching and Sorting Arrays
Search,Sort,Recursion.
C Arrays (2) (Chapter 6) ECET 264.
24 Searching and Sorting.
Search,Sort,Recursion.
EENG212 ALGORITHMS & DATA STRUCTURES
Searching.
Presentation transcript:

EENG212 Algorithms and Data Structures ARRAYS IN C EASTERN MEDITERRANEAN UNIVERSITY

Arrays in C OUTLINE ♦ Review of Arrays in C ♦ Declaration and Initialization of Arrays ♦ Sorting: Bubble Sort ♦ Searching: Linear and Binary Search

Arrays Arrays are defined to be a sequence/set of data elements of the same type. Having an array, each array element can be accessed by its position in the sequence of the array. ♦ Decleration of the Arrays: Any array declaration contains: the array name, the element type and the array size. Examples: int a[20], b[3],c[7]; float f[5], c[2]; char m[4], n[20];

Arrays Initialisation of an array is the process of assigning initial values. Typically declaration and initialisation are combined. Examples: int a[4]={1,3,5,2}; float, b[3]={2.0, 5.5, 3.14}; char name[4]= {‘E’,’m’,’r’,’e’}; int c[10]={0};

Example Write a program to calculate and print the average of the following array of integers. ( 4, 3, 7, -1, 7, 2, 0, 4, 2, 13) #include<stdio.h> #define size 10 int main() { int x[10]={4,3,7,-1,7,2,0,4,2,13}, i, sum=0; float av; for(i=0,i<=size-1;i++) sum = sum + x[i]; av = (float)sum/size; printf(“The average of the numbers=%.2f\n”, av); return 0; }

Sorting Sorting an array is the ordering the array elements in ascending (increasing -from min to max) or descending (decreasing – from max to min) order. Example: {2 1 5 3 2} {1, 2, 2, 3, 5} ascending order {2 1 5 3 2} {5, 3, 2, 2, 1} descending order

Bubble Sort Smaller values in the list gradually “bubble” their way upward to the top of the array. The technique makes several passes through the array. On each pass successive pairs of elements are compared. If the pair is in increasing order (or equal) the pair is unchanged. If a pair is in descending order, their values are swapped in the array:

Bubble Sort Pass = 1 Pass = 2 Pass = 3 Pass=4 2 1 5 3 2 1 2 3 2 5 1 2 2 3 5 1 2 5 3 2 1 2 3 5 2 Underlined pairs show the comparisons. For each pass there are size-1 comparisons. Total number of comparisons= (size-1)2

Bubble Sort C Code /* This program sorts the array elements in the ascending order*/ #include <stdio.h> #define SIZE 5 void BubbleSort(int [ ]); int main() { int a[SIZE]= {2,1,5,3,2}; int i; printf(“The elements of the array before sorting\n”); for (i=0; i<= SIZE-1; i++) printf(“%4d”, a[i]); BubbleSort(a); printf(“\n\nThe elements of the array after sorting\n”); return 0; }

void BubbleSort(int A[ ]) { int i, pass, hold; for (pass=1; pass<= SIZE-1; pass++){ for (i=0; i<= SIZE-2; i++) { if(A[i] >A[i+1]){ hold =A[i]; A[i]=A[i+1]; A[i+1]=hold; }

Example Write a program to determine the median of the array given below: (9, 4, 5, 1, 7, 78, 22, 15, 96, 45) Note that the median of an array is the middle element of a sorted array.

Example /* This program determines the median of an array*/ #include <stdio.h> #define SIZE 10 void BubbleSort(int []); int main() { int a[SIZE]= {9, 4, 5, 1, 7, 78, 22, 15, 96, 45}, median; int i; printf(“The elements of the input array is\n”); for (i=0; i<= SIZE-1; i++) printf(“%4d”, a[i]); BoubleSort(a); printf(“The elements of the sorted array is\n”); median = a[SIZE/2]; printf(“The median of the array is=%d”, median); return 0; }

Example void BubbleSort(int A[ ]) { int i, pass, hold; for (pass=1; pass<= SIZE-1; pass++){ for (i=0; i<= SIZE-2; i++) { if(A[i] >A[i+1]){ hold =A[i]; A[i]=A[i+1]; A[i+1]=hold; }

Searching The process of finding a particular element of an array is called searching. There two popularsearching techniques: Linear search and binary search. The linear search compares each array element with the search key. If the search key is a member of the array, typically the location of the search key is reported to indicate the presence of the search key in the array. Otherwise, a sentinel value is reported to indicate the presence of the search key in the array.

Linear Search Each member of the array is visited until the search key is found. Example: Write a program to search for the search key entered by the user in the following array: (9, 4, 5, 1, 7, 78, 22, 15, 96, 45) You can use the linear search in this example.

Linear Search /* This program is an example of the Linear Search*/ #include <stdio.h> #define SIZE 10 int LinearSearch(int [], int); int main() { int a[SIZE]= {9, 4, 5, 1, 7, 78, 22, 15, 96, 45}, key, pos; printf(“Enter the Search Key\n”); scanf(“%d”,&key); pos = LinearSearch(a, key); if(pos == -1) printf(“The search key is not in the array\n”); else printf(“The search key %d is at location %d\n”, key, pos); return 0; }

int LinearSearch (int b[ ], int skey) { int i; for (i=0; i<= SIZE-1; i++) if(b[i] == skey) return i; return -1; }

Binary Search Given a sorted array Binary Search algorithm can be used to perform fast searching of a search key on the sorted array. The following program uses pointer notation to implement the binary search algorithm for the search key entered by the user in the following array: (3, 5, 9, 11, 15, 17, 22, 25, 37, 68)

Binary Search #include <stdio.h> #define SIZE 10 int BinarySearch(int [ ], int); int main() { int a[SIZE]= {3, 5, 9, 11, 15, 17, 22, 25, 37, 68}, key, pos; printf(“Enter the Search Key\n”); scanf(“%d”,&key); pos = BinarySearch(a, key); if(pos == -1) printf(“The search key is not in the array\n”); else printf(“The search key %d is at location %d\n”, key, pos); return 0; }

int BinarySearch (int A[], int skey) { int low=0,high=SIZE-1,middle; while(low <= high){ middle= (low+high)/2; if(skey == A[middle]) return middle; else if(skey <A[middle]) high = middle-1; else low = middle+1; } return -1;

Computational Complexity The Computational Complexity of the Binary Search algorithm is measured by the maximum (worst case) number of Comparisons it performs for searching operations. The searched array is divided by 2 for each comparison/iteration. Therefore, the maximum number of comparisons is measured by: log2(n) where n is the size of the array Example: If a given sorted array 1024 elements, then the maximum number of comparisons required is: log2(1024) = 10 (only 10 comparisons is enough)

Computational Complexity Note that the Computational Complexity of the Linear Search is the maximum number of comparisons you need to search the array. As you are visiting all the array elements in the worst case, then, the number of comparisons required is: n (n is the size of the array) Example: If a given an array of 1024 elements, then the maximum number of comparisons required is: n = 1024 (As many as 1024 comparisons may be required)