From 9947eecc132ef616af40494534e32b94d640ce07 Mon Sep 17 00:00:00 2001 From: thetaniyagupta Date: Tue, 16 Oct 2018 22:48:32 +0530 Subject: [PATCH] Added queue,circuar queue, quick sort --- Queue/CircularQueue.c | 99 ++++++++++++++++++++++++++++++++++ Queue/Queue.c | 85 +++++++++++++++++++++++++++++ Sorting/QUICK SORT/Quicksort.c | 73 +++++++++++++++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 Queue/CircularQueue.c create mode 100644 Queue/Queue.c create mode 100644 Sorting/QUICK SORT/Quicksort.c diff --git a/Queue/CircularQueue.c b/Queue/CircularQueue.c new file mode 100644 index 0000000..317b2a1 --- /dev/null +++ b/Queue/CircularQueue.c @@ -0,0 +1,99 @@ +#include + +#define SIZE 5 + +int items[SIZE]; +int front = -1, rear =-1; + +int isFull() +{ + if( (front == rear + 1) || (front == 0 && rear == SIZE-1)) return 1; + return 0; +} + +int isEmpty() +{ + if(front == -1) return 1; + return 0; +} + +void enQueue(int element) +{ + if(isFull()) printf("\n Queue is full!! \n"); + else + { + if(front == -1) front = 0; + rear = (rear + 1) % SIZE; + items[rear] = element; + printf("\n Inserted -> %d", element); + } +} + + +int deQueue() +{ + int element; + if(isEmpty()) { + printf("\n Queue is empty !! \n"); + return(-1); + } else { + element = items[front]; + if (front == rear){ + front = -1; + rear = -1; + } /* Q has only one element, so we reset the queue after dequeing it. ? */ + else { + front = (front + 1) % SIZE; + + } + printf("\n Deleted element -> %d \n", element); + return(element); + } +} + + + + +void display() +{ + int i; + if(isEmpty()) printf(" \n Empty Queue\n"); + else + { + printf("\n Front -> %d ",front); + printf("\n Items -> "); + for( i = front; i!=rear; i=(i+1)%SIZE) { + printf("%d ",items[i]); + } + printf("%d ",items[i]); + printf("\n Rear -> %d \n",rear); + } +} + +int main() +{ + // Fails because front = -1 + deQueue(); + + enQueue(1); + enQueue(2); + enQueue(3); + enQueue(4); + enQueue(5); + + // Fails to enqueue because front == 0 && rear == SIZE - 1 + enQueue(6); + + display(); + deQueue(); + + display(); + + enQueue(7); + display(); + + // Fails to enqueue because front == rear + 1 + enQueue(8); + + return 0; +} \ No newline at end of file diff --git a/Queue/Queue.c b/Queue/Queue.c new file mode 100644 index 0000000..3ce8be4 --- /dev/null +++ b/Queue/Queue.c @@ -0,0 +1,85 @@ +#include +#include +#include + + +struct Queue +{ + int front, rear, size; + unsigned capacity; + int* array; +}; + + +struct Queue* createQueue(unsigned capacity) +{ + struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue)); + queue->capacity = capacity; + queue->front = queue->size = 0; + queue->rear = capacity - 1; + queue->array = (int*) malloc(queue->capacity * sizeof(int)); + return queue; +} + + +int isFull(struct Queue* queue) +{ return (queue->size == queue->capacity); } + + +int isEmpty(struct Queue* queue) +{ return (queue->size == 0); } + + +void enqueue(struct Queue* queue, int item) +{ + if (isFull(queue)) + return; + queue->rear = (queue->rear + 1)%queue->capacity; + queue->array[queue->rear] = item; + queue->size = queue->size + 1; + printf("%d enqueued to queue\n", item); +} + + +int dequeue(struct Queue* queue) +{ + if (isEmpty(queue)) + return INT_MIN; + int item = queue->array[queue->front]; + queue->front = (queue->front + 1)%queue->capacity; + queue->size = queue->size - 1; + return item; +} + +// Function to get front of queue +int front(struct Queue* queue) +{ + if (isEmpty(queue)) + return INT_MIN; + return queue->array[queue->front]; +} + +// Function to get rear of queue +int rear(struct Queue* queue) +{ + if (isEmpty(queue)) + return INT_MIN; + return queue->array[queue->rear]; +} + +int main() +{ + struct Queue* queue = createQueue(1000); + + enqueue(queue, 10); + enqueue(queue, 20); + enqueue(queue, 30); + enqueue(queue, 40); + + printf("%d dequeued from queue\n\n", dequeue(queue)); + + printf("Front item is %d\n", front(queue)); + printf("Rear item is %d\n", rear(queue)); + + return 0; +} \ No newline at end of file diff --git a/Sorting/QUICK SORT/Quicksort.c b/Sorting/QUICK SORT/Quicksort.c new file mode 100644 index 0000000..a86979e --- /dev/null +++ b/Sorting/QUICK SORT/Quicksort.c @@ -0,0 +1,73 @@ +/* C implementation QuickSort */ +#include + +// A utility function to swap two elements +void swap(int* a, int* b) +{ + int t = *a; + *a = *b; + *b = t; +} + +/* This function takes last element as pivot, places + the pivot element at its correct position in sorted + array, and places all smaller (smaller than pivot) + to left of pivot and all greater elements to right + of pivot */ +int partition (int arr[], int low, int high) +{ + int pivot = arr[high]; // pivot + int i = (low - 1); // Index of smaller element + + for (int j = low; j <= high- 1; j++) + { + // If current element is smaller than or + // equal to pivot + if (arr[j] <= pivot) + { + i++; // increment index of smaller element + swap(&arr[i], &arr[j]); + } + } + swap(&arr[i + 1], &arr[high]); + return (i + 1); +} + +/* The main function that implements QuickSort + arr[] --> Array to be sorted, + low --> Starting index, + high --> Ending index */ +void quickSort(int arr[], int low, int high) +{ + if (low < high) + { + /* pi is partitioning index, arr[p] is now + at right place */ + int pi = partition(arr, low, high); + + // Separately sort elements before + // partition and after partition + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } +} + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + printf("n"); +} + +// Driver program to test above functions +int main() +{ + int arr[] = {10, 7, 8, 9, 1, 5}; + int n = sizeof(arr)/sizeof(arr[0]); + quickSort(arr, 0, n-1); + printf("Sorted array: n"); + printArray(arr, n); + return 0; +}