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

Post on 19-Dec-2015

234 views 2 download

Tags:

Transcript of 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

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 = 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

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

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

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

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

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

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

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

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

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

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;

}