Introduction to C Programming CE00312-1 Lecture 11 Sorting and Searching using Arrays.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
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.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
Stacks and Queues. 2 Stack and Queue ADT’s You should understand How are they different philosophically from arrays What are they used for How do you.
CHAPTER 7 Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
Data Structure Dr. Mohamed Khafagy.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
The Heap ADT In this section of notes you will learn about a new abstract data type, the heap, as well how heaps can be used.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
CS Data Structures Chapter 3 Stacks and Queues.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
Queues.
UNIT II Queue.
Queues Chapter 4.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Chapter 15 Lists Objectives
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
CSCE 3110 Data Structures & Algorithm Analysis
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Instructor: Mr.Vahidipour
structures and their relationships." - Linus Torvalds
Lecture 21 Stacks and Queues Richard Gesick.
CSC 143 Queues [Chapter 7].
ITEC 2620M Introduction to Data Structures
Abstract Data Type Abstract Data Type as a design tool
Python: Stacks and Queues (as an Array)
structures and their relationships." - Linus Torvalds
Presentation transcript:

Introduction to C Programming CE Lecture 11 Sorting and Searching using Arrays

Searching and sorting Lists of data often stored in arrays May be complex data Use arrays of structs Many standard processes require searching and sorting

Linear Search Suitable for sorted or unsorted list Starts at beginning of array and checks each element against a target Loops for number of items in the list Returns position in array of found item or value representing not found Position used to retrieve data

Linear Search Target value =  Position found = Index = 0 Index = 1 Index = 2 Index = 3

Linear Search Target value =  Position found = Index = 0 Index = 1 Index = 2 Index = Index = Index = 5

Searching an array of structs Same process Use one of the fields to search e.g. birthdays[listindex].month Returns position in array as before

Bubble Sort Sort needed for variety of reasons Can be ascending or descending Sorted data often required for more efficient searching algorithms Bubble sort one of the simplest Each complete pass results in the highest unsorted item ‘bubbling’ to the end.

First Pass Compare 0, Compare 1,2 Compare 2,3 Compare 3,4 Compare 4,5

Second Pass Compare 0,1 Compare 1,2 Compare 2,3 Compare 3,4

Third Pass Compare 0,1 Compare 1,2 Compare 2,3

Sorting array of structs Same principle as for single data arrays One of the fields is used to perform the comparison E.g. birthdays [i].month > birthdays[i+1].month If a swap is needed the whole structs are swapped

Abstract Data Types System stack and System Heap deployed by system calls (stack used in assembler) Stack concept can be implemented by programmer Known as Abstract Data Type or ADT Main types are Stacks and Queues Can be implemented using various data structures

Stack and Queue

Addition to a stack (push) push (item, stack) begin if top = n then stackfull else begin increment top stack(top) = item end end Deletion from a stack (pop) pop(item, stack) begin if top = 0 then stackempty else begin item = stack[top] top = top-1 end end

Array Implementation of Stack /* Implements a stack using an array */ #include int main(void) { int data, i, max; int top = 0; int stack[100]; printf("\nStack size? "); scanf("%d", &max); for (i = 0; i <= max; i++) { stack[i] = 0; } while (top <= max-1) { printf("\nEnter an integer: "); scanf("\n%d", &data); top++; stack[top] = data; } printf("\nPopping data off the stack:\n\n"); while (top >= 1) { printf("%d\n", stack[top]); top--; } printf("\n"); return 0; }

Deletion from a queue (dequeue) deleteq (item, queue) begin if front = rear then queueempty else begin item = q[front] front = front+1 end end Addition into a queue addq (item, queue) begin if rear=n then queuefull else begin rear :=rear+1; q[rear]:=item; end end

Array Implementation of Queue /* queue.c */ /* Implements a queue using an array */ /* queues and enqueues via functions */ #include /* Prototypes */ int enqueue(int, int [],int ); int dequeue(int, int [],int );

Array Implementation of Queue int main(void) { int i, max, response; int rear = 0, front = 0; /* set front & rear */ int queue[100]; printf("\nqueue size? "); scanf("%d", &max); for (i = 0; i <= max; i++) { queue[i] = 0; } printf(" enter a choice (zero to exit): "); scanf("%d", &response);

Array Implementation of Queue while (response != 0) { if (response == 1) { rear = enqueue(rear,queue,max); } else { front = dequeue(front, queue, rear); } printf(" enter a choice : "); scanf("%d",&response); } printf("\n"); return 0; }

Array Implementation of Queue int enqueue(int rear, int queue[],int max) { if (rear <= max) /* if rear not at end of queue */ { int value; printf("\nEnter an integer to add: "); scanf("\n%d", &value); queue[rear] = value; displayqueue(rear,queue); rear++;/* bump to next available slot */ } return rear; }

Array Implementation of Queue int dequeue(int front, int queue[],int rear) { if (front < rear) { printf("\nremoving data from front of queue:"); /* go back to first value added */ printf("%d\n", queue[front]); queue[front] = 0; displayqueue(rear-1,queue); front++; } return front; }