Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 11 Sorting and Searching using Arrays."— Presentation transcript:

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

2 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

3 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

4 Linear Search Target value = 10 134 28 10981  Position found = 3 134 28 10981 134 28 10981 134 28 10981 Index = 0 Index = 1 Index = 2 Index = 3

5 Linear Search Target value = 101 134 28 10981  Position found = -1 134 28 10981 134 28 10981 134 28 10981 Index = 0 Index = 1 Index = 2 Index = 3 134 28 10981 Index = 4 134 28 10981 Index = 5

6 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

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

8 First Pass 134 28 10981 134 28 10981 128 34 10981 128 10 34981 Compare 0,1 128 10 93481 Compare 1,2 Compare 2,3 Compare 3,4 Compare 4,5

9 Second Pass 128 10 93481 128 10 93481 110 28 93481 110 9 283481 Compare 0,1 Compare 1,2 Compare 2,3 Compare 3,4

10 Third Pass 110 9 283481 110 9 283481 19 10 283481 Compare 0,1 Compare 1,2 Compare 2,3

11 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

12 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

13 Stack and Queue

14 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

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

16 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

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

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

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

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

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


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

Similar presentations


Ads by Google