+ All Categories
Home > Documents > Introduction to C Programming CE00312-1 Lecture 11 Sorting and Searching using Arrays.

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

Date post: 19-Dec-2015
Category:
View: 225 times
Download: 2 times
Share this document with a friend
Popular Tags:
21
Introduction to C Programming CE00312-1 Lecture 11 Sorting and Searching using Arrays
Transcript
Page 1: Introduction to C Programming CE00312-1 Lecture 11 Sorting and Searching using Arrays.

Introduction to C Programming

CE00312-1

Lecture 11

Sorting and Searching using Arrays

Page 2: Introduction to C Programming CE00312-1 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

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

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

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

Linear Search

Target value = 10

1 34 28 10 9 81

Position found = 3

1 34 28 10 9 81

1 34 28 10 9 81

1 34 28 10 9 81

Index = 0

Index = 1

Index = 2

Index = 3

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

Linear Search

Target value = 101

1 34 28 10 9 81

Position found = -1

1 34 28 10 9 81

1 34 28 10 9 81

1 34 28 10 9 81

Index = 0

Index = 1

Index = 2

Index = 3

1 34 28 10 9 81 Index = 4

1 34 28 10 9 81 Index = 5

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

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

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

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.

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

First Pass

1 34 28 10 9 81

1 34 28 10 9 81

1 28 34 10 9 81

1 28 10 34 9 81

Compare 0,1

1 28 10 9 34 81

Compare 1,2

Compare 2,3

Compare 3,4

Compare 4,5

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

Second Pass

1 28 10 9 34 81

1 28 10 9 34 81

1 10 28 9 34 81

1 10 9 28 34 81

Compare 0,1

Compare 1,2

Compare 2,3

Compare 3,4

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

Third Pass

1 10 9 28 34 81

1 10 9 28 34 81

1 9 10 28 34 81

Compare 0,1

Compare 1,2

Compare 2,3

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

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

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

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

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

Stack and Queue

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

Addition to a stack (push)

push (item , stack)begin     if top = n then stackfull else begin increment top     stack(top) = item endend

Deletion from a stack (pop)

pop(item , stack)begin     if top = 0 then stackempty else begin     item = stack[top]      top = top-1 end end

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

Array Implementation of Stack/* Implements a stack using an array */

#include <stdio.h>

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

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

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;

    endend

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

Array Implementation of Queue

/* queue.c *//* Implements a queue using an array *//* queues and enqueues via functions */

#include <stdio.h>

/* Prototypes */int enqueue(int, int [],int );int dequeue(int, int [],int );

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

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

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

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

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

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;

}

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

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;

}


Recommended