What is Queue
Queue is a abstract data type with two operation add_data(enque) and get_data(deque) and provides the data in the form of its arrival or simply it maintains the first in first out machenism (FIFO). Data is always added to its top and retrieved from its rear.
Required Functions
int queue_size()
struct node * create_node(int data)
void add_data(int data)
int get_data()
void traverse_queue()
Sample Code
struct node
{
int data;
struct node *next;
};
struct node *top, *rear;
int length= 0;
int queue_size()
{
printf("\n No. of elements in queue : %d", length);
return length;
}
struct node * create_node(int data)
{
struct node *new;
new = (struct node*)malloc(sizeof(struct node));
new->next = null;
new->data = data;
length++;
return new;
}
void add_data(int data)
{
struct node *new;
new = create_node(data);
if ((top == NULL) && (rear == NULL))
{
top = new;
rear = new;
}
else
{
top->next = new;
top = new;
}
}
int get_data()
{
struct node *temp_rear;
int ret_val;
temp_rear = rear;
if (temp_rear == NULL)
{
printf("\n Error : Trying to pop from empty queue");
return -1; // Assuming -1 as error
}
else
{
temp_rear = temp_rear->next;
}
printf("\n Popped value : %d", rear->data);
ret_val = rear->data;
free(rear);
rear = temp_rear;
length--;
return ret_val;
}
void traverse_queue()
{
struct node *temp_rear;
temp_rear = top;
if (temp_rear == NULL)
{
printf("Queue is empty");
return;
}
while (temp_rear != NULL)
{
printf("%d ", temp_rear->data);
temp_rear = temp_rear->next;
}
}
Quick sort is a very popular sorting method. This sort name comes from the fact that quick sort can sort a list of data elements significantly faster than any of the common sorting algorithms. This sorting uses a strategy called divide and conquer. It is based on the principle that is faster and easier to sort two small arrays than one larger one.
The sequence of steps required to perform a quick sort operation on a set of elements of the array are as follows.
A pivot item near the middle of the array is chosen. Let it be x.
Now scan the array from left to right until some element e.g., ai>x is encountered.
Now scan the array from right to left until some element e.g.,aj<x is encountered.
Now exchange the items ai and aj.
Again repeat steps 2 to 4 until the scan operation meets somewhere in the middle of the array.
Quick sort is also called partition exchange sort and it was invented by C.A.R. Hoare. The average execution time of quick sort is O(n(log2n)2) and this sort is good for large sorting jobs.
Pictorial Representation
Quick Sort Algorithm
1. If n < = 1, then return.
2. Pick any element V in a[]. This is called the pivot.
3. Rearrange elements of the array by moving all elements xi > V right of V and all elements xi < = V left of V. If the place of the V after re-arrangement is j, all elements with value less than V, appear in a[0], a[1] . . . . a[j – 1] and all those with value greater than V appear in a[j + 1] . . . . a[n – 1].
4. Apply quick sort recursively to a[0] . . . . a[j – 1] and to a[j + 1] . . . . a[n – 1].
Program for Quick Sort
#include <stdio.h>
void quick_sort(int[], int, int); int partition(int[], int, int);
int main() { int a[50], n, i;
printf("How many elements?"); scanf("%d", & n); printf("\nEnter array elements:");