0% found this document useful (0 votes)
41 views13 pages

Circular Queue Implementation in C

The document contains programs for various sorting and searching algorithms: 1. Circular queue implementation using an array with functions to add and delete elements. 2. Bubble sort to sort an integer array. 3. Selection sort using a function to find the minimum element and swap. 4. Quicksort using partitioning and recursion to sort an integer array. 5. Shell sort as an extension of insertion sort using incrementing gap sequences. 6. Insertion sort to insert elements into sorted position in the array. 7. Linear search to search for an element in an unsorted array. 8. Binary search on a sorted array using recursion and partitioning.

Uploaded by

Swami Sainath
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views13 pages

Circular Queue Implementation in C

The document contains programs for various sorting and searching algorithms: 1. Circular queue implementation using an array with functions to add and delete elements. 2. Bubble sort to sort an integer array. 3. Selection sort using a function to find the minimum element and swap. 4. Quicksort using partitioning and recursion to sort an integer array. 5. Shell sort as an extension of insertion sort using incrementing gap sequences. 6. Insertion sort to insert elements into sorted position in the array. 7. Linear search to search for an element in an unsorted array. 8. Binary search on a sorted array using recursion and partitioning.

Uploaded by

Swami Sainath
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

6.

Program for Circular Queue implementation through Array

#include<stdio.h> #include<ctype.h> # define maxsize 6 int cq[maxsize]; int front,rear; void main() { void add(int,int[],int,int,int); int del(int[],int,int,int); int will=1,i,num; front=0; rear=0; printf("\n program for circular queue through array \n \n"); while(will==1) { printf("\n press 1 to add element to circular queue\n"); printf("\n press 2 to delete element to circular queue\n"); scanf("%d",&will); switch(will) { case 1: printf("\n enter the data...\n"); scanf("%d",&num); rear++;

add(num,cq,maxsize,front,rear); break; case 2: i=del(cq,maxsize,front,rear); printf("\n the value retreived from circular queue is: %d",i); break; default:printf("invalid choice"); } printf("\n\n do you want to do more operations on circular queue\n"); printf("\n press 1 for yes,any other key to exit"); scanf("%d",&will); } } void add(int item,int q[],int max,int front,int rear) {rear=(rear%max); if(front==rear) { printf("cicular queue full"); return; } else { cq[rear]=item; printf("rear=%d front=%d",rear,front); }

} int del(int q[],int max,int front,int rear) { int a; if(front==rear) { printf("circular queue empty"); return(0); } else { front++; front=front%max; a=cq[front]; return(a); printf("rear=%d front=%d",rear,front); } }
7.

Write a C program to sorting the given array using bubble sort.

#include<stdio.h> void main() { int a[20], n, temp, i, j; printf("\nEnter the total number: "); scanf("%d",&n); printf("\nEnter the elements of the array:\n"); for(i=1; i<=n; i++) { scanf("%d", &a[i]); }

for(i=1; i<=n-1; i++) for(j=1; j<=n-i;j++) if(a[j]>a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } printf("\nTHE ASCENDING ORDER LIST IS:"); for(i=1; i<=n; i++) printf("\n%d",a[i]); }

Enter the total number: 5 Enter the elements of the array: 5 4 3 2 1


8.

Write a program to implement Selection sort.

#include<stdio.h> #include<conio.h> #define MAXSIZE 500 void selection(int elements[], int maxsize); int a[MAXSIZE],maxsize; int main() { int i;

printf("\nEnter the size of the array: "); scanf("%d",&maxsize); printf("\nEnter the values : "); for (i = 0; i < maxsize; i++) { scanf("%d",&a[i]); } printf("\nArray before sorting:\n"); for (i = 0; i < maxsize; i++) printf("%d\t",a[i]); printf ("\n"); selection(a, maxsize); printf("\nArray after sorting:\n"); for (i = 0; i < maxsize; i++) printf("%d\t", a[i]); } void selection(int a[], int array_size) { int i, j, k; int min, temp; for (i = 0; i < maxsize-1; i++) { min = i; for (j = i+1; j < maxsize; j++) {

if (a[j] < a[min]) min = j; } temp =a[i]; a[i] = a[min]; a[min] = temp; } }

9.

Write a program to implement Quick sort.

#include<stdio.h> #include<conio.h>

void quicksort(int [],int,int); int partition(int [],int,int); main() { int a[20],p,q,i,n; clrscr();

printf("Enter The number Of Elements\t: "); scanf("%d",&n); for(i=0;i< n;i++) { scanf("%d",&a[i]); } p=0; q=n-1; printf("\nArray Befor Sorting : "); for(i=0;i< n;i++) printf("%5d",a[i]); quicksort(a,p,q);

printf("\nArray After Sorting : "); for(i=0;i< n;i++) printf("%5d",a[i]); getch(); return 0; } void quicksort(int a[],int p,int q) { int j; if(p< q) { j=partition(a,p,q+1); quicksort(a,p,j-1); quicksort(a,j+1,q); } } int partition(int a[],int m,int p) { int v,i,j; int temp; v=a[m]; i=m;j=p; do { do

{ i += 1; } while(a[i]< v); do { j -= 1; } while(a[j]>v);

if(i< j) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } while(i< j); a[m] =a[j]; a[j] = v; return j; }
10. Write

a program to implement Shell sort.

#include<stdio.h> #include<conio.h> void shellsort(int a[],int n)

{ int j,i,m,mid; for(m = n/2;m>0;m/=2) { for(j = m;j< n;j++) { for(i=j-m;i>=0;i-=m) { if(a[i+m]>=a[i]) break; else { mid = a[i]; a[i] = a[i+m]; a[i+m] = mid; } } } } } main() { int a[10],i,n; clrscr(); printf("Enter The number Of Elements\t: "); scanf("%d",&n); for(i=0;i< n;i++) { printf("\nElement %d\t: ",i+1); scanf("%d",&a[i]); } printf("\nArray Befor Sorting : "); for(i=0;i< n;i++) printf("%5d",a[i]); shellsort(a,n); printf("\nArray After Sorting : "); for(i=0;i< n;i++) printf("%5d",a[i]); getch(); return 0; }

11. Write

a program to implement insertion sort.

#include<stdio.h> #include<conio.h> void main() { int A[20], N, Temp, i, j; clrscr(); printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { gotoxy(25,11+i); scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp<A[j] && j>=0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); }

12. Write

a program to implement linear search.

#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,n,item,a[20]; printf("\nEnter no of elements="); scanf("%d",&n); printf("\nEnter %d elements=",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nEnter the element to be search="); scanf("%d",&item); for(i=0;i<n;i++) { if(a[i]==item) { printf("\n\t%d is present at position %d",a[i],i+1); break; } } if(i==n) printf("\nElement is not Present"); getch(); }
13. Write

a program to implement binary search.

#include <stdio.h> #define TRUE 0 #define FALSE 1 int main(void) { int array[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int left = 0; int right = 10; int middle = 0; int number = 0; int bsearch = FALSE;

int i = 0; printf("ARRAY: "); for(i = 1; i <= 10; i++) printf("%d ", i*10); printf("\nSearch for Number: "); scanf("%d", &number); while(bsearch == FALSE && left <= right) { middle = (left + right) / 2; if(number == array[middle]) { bsearch = TRUE; printf("Number found in %d position of array\n",middle+1); } else { if(number < array[middle]) right = middle - 1; if(number > array[middle]) left = middle + 1; } } if(bsearch == FALSE) printf("-- Number Not found --\n"); return 0; }

Common questions

Powered by AI

The partitioning process in quicksort involves selecting a 'pivot' element and rearranging the array elements such that all elements less than the pivot are on its left, and all greater elements are on the right. This ensures that the pivot is in its final sorted position. The efficiency of quicksort arises from its recursive application of this partitioning to sub-arrays, reducing the problem size with each division, and ideally resulting in a balanced partitioning that allows it to reach O(n log n) complexity. Poorly chosen pivots may lead to unbalanced partitions and O(n^2) worst-case performance .

Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if in the wrong order, performing O(n^2) comparisons in the worst case. Quicksort, on the other hand, utilizes a divide-and-conquer approach, selecting a 'pivot' element to partition the array into sub-arrays and recursively sorting them, achieving an average time complexity of O(n log n) but O(n^2) in the worst case if pivot choices are poor . Quicksort generally performs better on large datasets due to its average-case efficiency compared to bubble sort.

In both linear and binary search implementations, an out-of-bounds error could arise from incorrectly using array indices, particularly if index calculations do not consider the bounds of the array. This could lead to accessing memory locations beyond the intended array, potentially causing a segmentation fault or crash. In binary search, incorrect handling of indices when adjusting 'left' and 'right' pointers could prevent convergence and result in infinite loops .

The circular nature of a circular queue allows for the reuse of queue positions once elements are dequeued from the front, which is not possible in a typical queue after the front has moved past the initial array positions. This reduces the likelihood of unused spaces in the queue, thus enhancing memory efficiency and allowing continuous operations despite constraints on maximum queue size . It enables better space utilization than a linear queue, which might end up with a lot of 'wasted' slots once elements are removed.

Shell sort improves upon insertion sort by allowing the exchange of far apart elements and decreasing the total number of swaps required in insertion sort. It uses a technique called 'gap insertion sort', where the list is initially sorted using elements at a certain interval (gap), gradually reducing this gap and performing regular insertion sort as the final step. This approach significantly reduces the number of movements needed to sort the array, enhancing performance over traditional insertion sort .

Binary search operates by dividing the sorted array in half each time to look for the target element, reducing the search space exponentially (O(log n) complexity), while linear search checks each element sequentially, resulting in O(n) complexity. However, for binary search to be applicable, the array or list must be sorted beforehand, which adds a constraint not present in linear search . This makes binary search significantly more efficient for large datasets when the sort constraint is met.

In a circular queue implemented through an array, overflow is handled by wrapping around when the end of the array is reached, thus efficiently utilizing available space. When the 'rear' becomes equal to the 'front', the queue is considered full regardless of free space remaining in the array, preventing overflow . This contrasts with a linear queue, where additional space may not be used efficiently once the rear reaches the array's end, resulting in a scenario called 'false overflow'.

Insertion sort can be preferable for small datasets due to its simple implementation and lower overhead, performing adequately with a time complexity of O(n^2) because of the fewer number of elements needing sorting. It requires minimal additional memory and efficiently handles pre-sorted or nearly sorted data, often outperforming more complex algorithms on small arrays because its simpler operations engage lesser computational overhead .

The inner loop in a selection sort implementation is necessary to find the index of the minimum element in the unsorted part of the array. This index is then used to swap the smallest element to the front of the unsorted section. This process is repeated for the remaining unsorted elements, ensuring that the array gradually becomes sorted with each iteration of the outer loop .

Implementing the shell sort algorithm presents challenges in selecting the sequence of gaps, which significantly affects performance. An inefficient gap sequence can lead to longer-than-expected runtime. Mitigating this involves using sequences with proven efficiency like the Knuth or Sedgewick sequences. Proper testing and profiling are necessary to refine the implementation and ensure optimal performance across varying input sizes .

You might also like