0% found this document useful (0 votes)
15 views46 pages

C Pgms

The document contains multiple C programming examples covering various data structures and algorithms including sorting strings, array operations (insertion, deletion, searching), pattern matching, appending arrays, linear and binary search, sparse matrix representation, linked lists (singly and doubly), and their respective display functions. Each example includes code snippets demonstrating the implementation of these concepts. The document serves as a practical guide for understanding fundamental programming techniques in C.

Uploaded by

ssoulstore.ss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views46 pages

C Pgms

The document contains multiple C programming examples covering various data structures and algorithms including sorting strings, array operations (insertion, deletion, searching), pattern matching, appending arrays, linear and binary search, sparse matrix representation, linked lists (singly and doubly), and their respective display functions. Each example includes code snippets demonstrating the implementation of these concepts. The document serves as a practical guide for understanding fundamental programming techniques in C.

Uploaded by

ssoulstore.ss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

1. Sort a given list of strings.

#include<string.h>
void main()
{
int i,j,count;
char str[20][20],temp[20];
printf("enter the no. of string: ");
scanf("%d",&count);
printf("enter strings one by one: \n");
for(i=0;i<=count;i++)
gets(str[i]);
for(i=0;i<=count;i++){
for(j=i+1;j<=count;j++){
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}}}
printf("\n sorted string:");
for(i=0;i<=count;i++)
puts(str[i]);
return 0;
}

2. Insertion and deletion operation in an array.

#include <stdio.h>

#include <stdlib.h>

int main()

int a[100];

int element,i,loc,size,n=0,j=0;

printf("Enter the size of an array\n");

scanf("%d",&size);

printf("Enter %d array elements\n",size);

for(i=0;i<size;i++)

scanf("%d",&a[i]);

}
printf("List before Insertion: ");

for(i=0;i<size;i++)

printf("%d ",a[i]);

printf("\nEnter an element to insert\n");

scanf("%d",&element);

printf("Enter a position to insert an element %d\n",element);

scanf("%d",&loc);

loc--;

for(i=size-1;i>=loc;i--)

a[i+1]=a[i];

a[loc]=element;

printf("\nList after Insertion: ");

for(i=0;i<size+1;i++)

printf("%d ",a[i]);

printf("\nEnter an element to delete\n");

scanf("%d",&n);

i=0;

for(i=0;i<size;i++)

if(a[i]==n)

for(j=i;j<(size-1);j++)

a[j]=a[j+1];

}
break;

printf("List after deletion\n");

for(i=0;i<(size-1);i++)

printf("%d ",a[i]);

return 0;

3. Search an element in the 2-dimensional array.

#include<stdio.h>

int main(){

int m, n, item, count=0, array[10][10];

printf("Enter the number of rows and columns: ");

scanf("%d %d", &m, &n);

printf("Enter %d elements: ", (m*n));

for(int i=0; i<m; i++){

for(int j=0; j<n; j++){

scanf("%d", &array[i][j]);

printf("Enter the item to find: ");

scanf("%d", &item);

for(int i=0; i<m; i++){

for(int j=0; j<n; j++){

if(array[i][j] == item){

printf("Item found at [%d, %d] \n", i, j);

count++;

}
}

if(count==0)

printf("Item Not found");

return 0;

4. Implement Pattern matching algorithm.

#include <stdio.h>

#include <string.h>

int main()

char text[20],pat[20];

int a,b;

printf("Enter the string : ");

gets(text);

printf("Enter the pattern to find : ");

gets(pat);

a = strlen(pat);

b = strlen(text);

for (int i = 0; i <= b - a; i++) {

int j;

for (j = 0; j < a; j++)

if (text[i + j] != pat[j])

break;

if (j == a)

printf("Pattern found at position %d \n", i+1);

}
return 0;

5. Append 2 arrays.

#include<stdio.h>

#include<string.h>

int main() {

int a[20],b[20],c[40],a_len,b_len,i,j,k,m,n;

printf("Enter number of elements in first array : ");

scanf("%d",&m);

printf("Enter the elements : ");

for(i = 0; i < m; i++) {

scanf("%d",&a[i]);

printf("Enter number of elements in second array : ");

scanf("%d",&n);

printf("Enter the elements : ");

for(i = 0; i < n; i++) {

scanf("%d",&b[i]);

for(i = 0; i < m; i++) {

c[i]=a[i];

for(j = 0; j < n; j++) {

c[j+m]=b[j];

printf("Array one ");

for(i=0;i<m;i++){

printf("%d ",a[i]);
}

printf("\n");

printf("Array two ");

for(i=0;i<n;i++){

printf("%d ",b[i]);

printf("\n");

printf("Arrays after appending : ");

for(i=0;i<m+n;i++){

printf("%d ",c[i]);

return 0;

6. Search an element in the array using linear search.

#include <stdio.h>

void main()

int num;

int i, key, element_found = 0;

printf("Enter number of elements you would like to take as input: ");

scanf("%d", &num);

int arr[num];

printf("\nEnter all the elements of your choice:");


for (i = 0; i < num; i++)

scanf("%d", &arr[i]);

printf("\nEnter the key element that you would like to be searched: ");

scanf("%d", &key);

for (i = 0; i < num ; i++)

if (key == arr[i] )

element_found = 1;

break;

if (element_found == 1)
printf("we got the element at index %d",i+1);

else

printf("we haven’t got element at any index in the array\n");

7. Search an element in the array using binary search.

int main()

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d",&n);

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )

scanf("%d",&array[c]);

printf("Enter value to find\n");

scanf("%d",&search);

first = 0;

last = n - 1;

middle = (first+last)/2;

while( first <= last )

if ( array[middle] < search )

first = middle + 1;

else if ( array[middle] == search )

printf("%d found at location %d.\n", search, middle+1);

break;

}
else

last = middle - 1;

middle = (first + last)/2;

if ( first > last )

printf("Not found! %d is not present in the list.\n", search);

return 0;

8. Read a sparse matrix and display its triplet representation using array.

#include<stdio.h>

int main()

int S[10][10],m,n,i,k=0,size=0;

printf("Enter number of rows in the matrix : ");

scanf("%d",&m);

printf("Enter number of columns in the matrix : ");

scanf("%d",&n);

printf("Enter elements in the matrix : ");

for (int i = 0; i < m; i++)

for (int j = 0; j < n; j++)

scanf("%d",&S[i][j]);

printf("The matrix is \n");

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

printf(" %d ",S[i][j]);

if (S[i][j] != 0)

size++;

printf("\n");

int M[3][size];
for (int i = 0; i < m; i++)

for (int j = 0; j < n; j++)

if (S[i][j] != 0)

M[0][k] = i;

M[1][k] = j;

M[2][k] = S[i][j];

k++;

printf("Triplet representation of the matrix is \n");

for (int i=0; i<3; i++)

for (int j=0; j<size; j++)

printf(" %d ", M[i][j]);

printf("\n");

return 0;

9. Create a singly linked list of n nodes and display it.

#include <stdio.h>

#include <stdlib.h>

struct node {

int num;

struct node *nextptr;

} *stnode;

void createNodeList(int n);

void displayList();

int main() {

int n;
printf("\n\n Linked List : To create and display Singly Linked List :\n");

printf("-------------------------------------------------------------\n");

printf(" Input the number of nodes : ");

scanf("%d", &n);

createNodeList(n);

printf("\n Data entered in the list : \n");

displayList();

return 0;

void createNodeList(int n) {

struct node *fnNode, *tmp;

int num, i;

stnode = (struct node *)malloc(sizeof(struct node));

if(stnode == NULL) {

printf(" Memory can not be allocated.");

} else {

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode->num = num;

stnode->nextptr = NULL;

tmp = stnode;

for(i = 2; i <= n; i++) {

fnNode = (struct node *)malloc(sizeof(struct node));

if(fnNode == NULL) {

printf(" Memory can not be allocated.");

break;

} else {
printf(" Input data for node %d : ", i);

scanf(" %d", &num);

fnNode->num = num;

fnNode->nextptr = NULL;

tmp->nextptr = fnNode;

tmp = tmp->nextptr;

void displayList() {

struct node *tmp;

if(stnode == NULL) {

printf(" List is empty.");

} else {

tmp = stnode;

while(tmp != NULL) {

printf(" Data = %d\n", tmp->num);

tmp = tmp->nextptr;

10. Delete a given node from a singly linked list.


#include <stdio.h>

#include <malloc.h>

struct node

int value;

struct node *next;

};

typedef struct node snode;

snode *newnode, *ptr;

snode *first = NULL, *last = NULL, *prev = NULL;

snode* create_node(int);

void insert_node(int);

void display();

void delete_node(int);

int main()

int ch=0,n,item;

while (ch!=4)

printf("\n---Linked List Deletion---");

printf("\n1.Create List \n2.Display List \n3.Delete Node \n4.Exit\n");

printf("\nEnter your choice : ");

scanf("%d", &ch);

switch (ch)

case 1:

printf("\nEnter the number of elements : ");

scanf("%d",&n);

insert_node(n);
break;

case 2:

printf("\nElements in the list are : ");

display();

break;

case 3:

printf("Enter the element to delete : ");

scanf("%d",&item);

delete_node(item);

break;

case 4:

printf("Exiting...");

break;

default:

printf("\nInvalid Choice\n");

break;

return 0;

snode* create_node(int val)

newnode = (snode *)malloc(sizeof(snode));

newnode->value = val;

newnode->next = NULL;

return newnode;

void insert_node(int n)

first = NULL,last=NULL;

int val,i;
for(i=0;i<n;i++)

printf("\nEnter the value for the Node %d : ",i+1);

scanf("%d", &val);

newnode = create_node(val);

if (first == last && last == NULL)

first = last = newnode;

first->next = NULL;

last->next = NULL;

else

last->next = newnode;

last = newnode;

last->next = NULL;

printf("\nLinked list created\n");

void display()

if (first == NULL)

printf("\nNo nodes in the list to display\n");

else

for (ptr = first;ptr != NULL;ptr = ptr->next)

printf(" %d ", ptr->value);


}

void delete_node(int item)

int i,pos=0;

struct node *temp,*ptr=first;

if(first==NULL)

printf("nThe List is Empty:n");

else

while(ptr != NULL)

if(ptr->value == item)

break;

pos++;

ptr = ptr->next;

if(pos==0)

ptr=first;

first=first->next ;

printf("The deleted element is:%d",ptr->value);

free(ptr);

else

ptr=first;

for(i=0;i<pos;i++){
temp=ptr;

ptr=ptr->next ;

temp->next =ptr->next ;

printf("The deleted element is:%d",ptr->value);

free(ptr);

11. Create a doubly linked list of integers and display in forward and backward direction.

#include<stdio.h>

#include<stdlib.h>

struct node

struct node *prev;

struct node *next;

int data;

};

struct node *head;

void insertion(int);

void display_forward();

void display_backward();

int main()

int choice =0,n;

while(choice != 4)

printf("\nDoubly Linked List\n");

printf("\n1.Create List\n2.Display in Forward Order\n3.Display in Backward Order\n4.Exit");

printf("\nEnter your choice : ");

scanf("\n%d",&choice);
switch(choice)

case 1:

printf("\nEnter the number of elements : ");

scanf("%d",&n);

insertion(n);

break;

case 2:

display_forward();

break;

case 3:

display_backward();

break;

case 4:

exit(0);

break;

default:

printf("Please enter valid choice..");

return 0;

void insertion(int n)

head = NULL;

struct node *ptr,*temp;

int item,i;

for(i=0;i<n;i++)

ptr = (struct node *) malloc(sizeof(struct node));

if(ptr == NULL)
{

printf("\nOVERFLOW");

else

printf("\nEnter value %d : ",i+1);

scanf("%d",&item);

ptr->data=item;

if(head == NULL)

ptr->next = NULL;

ptr->prev = NULL;

head = ptr;

else

temp = head;

while(temp->next!=NULL)

temp = temp->next;

temp->next = ptr;

ptr ->prev=temp;

ptr->next = NULL;

printf("\nLinked list created\n");

void display_forward()

{
struct node *ptr;

printf("Elements in the linked list (forward) : ");

ptr = head;

while(ptr != NULL)

printf("%d\t",ptr->data);

ptr=ptr->next;

void display_backward()

struct node *ptr,*last;

printf("Elements in the linked list (backward) : ");

ptr = head;

while(ptr != NULL)

last = ptr;

ptr=ptr->next;

while(last != NULL)

printf("%d\t",last->data);

last=last->prev;

12. Implement Stack using array.

#include<stdio.h>

int stack[100],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);
int main()

top=-1;

printf("\n Enter the size of STACK[MAX=100]:");

scanf("%d",&n);

printf("\n\t STACK OPERATIONS USING ARRAY");

printf("\n\t--------------------------------");

printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

do

printf("\n Enter the Choice:");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

{
printf("\n\t EXIT POINT ");

break;

default:

printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");

while(choice!=4);

return 0;

void push()

if(top>=n-1)

printf("\n\tSTACK is over flow");

else

printf(" Enter a value to be pushed:");

scanf("%d",&x);

top++;

stack[top]=x;

void pop()

if(top<=-1)
{

printf("\n\t Stack is under flow");

else

printf("\n\t The popped elements is %d",stack[top]);

top--;

void display()

if(top>=0)

printf("\n The elements in STACK \n");

for(i=top; i>=0; i--)

printf("\n%d",stack[i]);

printf("\n Press Next Choice");

else

printf("\n The STACK is empty");

13. Implement Stack using linked list.

#include <stdio.h>

#include <stdlib.h>

void push();

void pop();

void display();

struct node
{

int val;

struct node *next;

};

struct node *head;

void main ()

int choice=0;

printf("\n*********Stack operations using linked list*********\n");

printf("\n----------------------------------------------\n");

while(choice != 4)

printf("\n\nChose one from the below options...\n");

printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");

printf("\n Enter your choice \n");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

{
display();

break;

case 4:

printf("Exiting....");

break;

default:

printf("Please Enter valid choice ");

};

void push ()

int val;

struct node *ptr = (struct node*)malloc(sizeof(struct node));

if(ptr == NULL)

printf("not able to push the element");

else

printf("Enter the value");

scanf("%d",&val);

if(head==NULL)

ptr->val = val;

ptr -> next = NULL;


head=ptr;

else

ptr->val = val;

ptr->next = head;

head=ptr;

printf("Item pushed");

void pop()

int item;

struct node *ptr;

if (head == NULL)

printf("Underflow");

else

item = head->val;

ptr = head;

head = head->next;

free(ptr);

printf("Item popped");

}
}

void display()

int i;

struct node *ptr;

ptr=head;

if(ptr == NULL)

printf("Stack is empty\n");

else

printf("Printing Stack elements \n");

while(ptr!=NULL)

printf("%d\n",ptr->val);

ptr = ptr->next;

14. Evaluation of postfix expressions.

#include<stdio.h>

int stack[20];

int top = -1;

void push(int x)

stack[++top] = x;

int pop()
{

return stack[top--];

int main()

char exp[20];

char *e;

int n1,n2,n3,num;

printf("Enter the expression :: ");

scanf("%s",exp);

e = exp;

while(*e != '\0')

if(isdigit(*e))

num = *e - 48;

push(num);

else

n1 = pop();

n2 = pop();

switch(*e)

case '+':

n3 = n1 + n2;

break;

case '-':
{

n3 = n2 - n1;

break;

case '*':

n3 = n1 * n2;

break;

case '/':

n3 = n2 / n1;

break;

push(n3);

e++;

printf("\nThe result of expression %s = %d\n\n",exp,pop());

return 0;

15. Implement Queue using array.

#include<stdio.h>

#include<stdlib.h>

#define maxsize 5

void insert();

void delete();

void display();

int front = -1, rear = -1;

int queue[maxsize];
void main ()

int choice;

while(choice != 4)

printf("\n*************************Main Menu*****************************\n");

printf("\n=================================================================\n");

printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");

printf("\nEnter your choice ?");

scanf("%d",&choice);

switch(choice)

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf("\nEnter valid choice??\n");

void insert()

{
int item;

printf("\nEnter the element\n");

scanf("\n%d",&item);

if(rear == maxsize-1)

printf("\nOVERFLOW\n");

return;

if(front == -1 && rear == -1)

front = 0;

rear = 0;

else

rear = rear+1;

queue[rear] = item;

printf("\nValue inserted ");

void delete()

int item;

if (front == -1 || front > rear)

printf("\nUNDERFLOW\n");

return;

else
{

item = queue[front];

if(front == rear)

front = -1;

rear = -1 ;

else

front = front + 1;

printf("\nvalue deleted ");

void display()

int i;

if(rear == -1)

printf("\nEmpty queue\n");

else

{ printf("\nprinting values .....\n");

for(i=front;i<=rear;i++)

printf("\n%d\n",queue[i]);

}
}

16. Implement Queue using linked list.

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *front;

struct node *rear;

void insert();

void delete();

void display();

void main ()

int choice;

while(choice != 4)

printf("\n*************************Main Menu*****************************\n");

printf("\n=================================================================\n");

printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");

printf("\nEnter your choice ?");

scanf("%d",& choice);

switch(choice)

case 1:

insert();

break;

case 2:

delete();
break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf("\nEnter valid choice??\n");

void insert()

struct node *ptr;

int item;

ptr = (struct node *) malloc (sizeof(struct node));

if(ptr == NULL)

printf("\nOVERFLOW\n");

return;

else

printf("\nEnter value?\n");

scanf("%d",&item);

ptr -> data = item;

if(front == NULL)

front = ptr;
rear = ptr;

front -> next = NULL;

rear -> next = NULL;

else

rear -> next = ptr;

rear = ptr;

rear->next = NULL;

void delete ()

struct node *ptr;

if(front == NULL)

printf("\nUNDERFLOW\n");

return;

else

ptr = front;

front = front -> next;

free(ptr);

void display()

struct node *ptr;

ptr = front;
if(front == NULL)

printf("\nEmpty queue\n");

else

{ printf("\nprinting values .....\n");

while(ptr != NULL)

printf("\n%d\n",ptr -> data);

ptr = ptr -> next;

17. Traverse a binary search tree in preorder.

#include <stdio.h>

#include <stdlib.h>

struct node {

int element;

struct node* left;

struct node* right;

};

struct node* createNode(int val)

struct node* Node = (struct node*)malloc(sizeof(struct node));

Node->element = val;

Node->left = NULL;

Node->right = NULL;
return (Node);

void traversePreorder(struct node* root)

if (root == NULL)

return;

printf(" %d ", root->element);

traversePreorder(root->left);

traversePreorder(root->right);

int main()

struct node* root = createNode(40);

root->left = createNode(30);

root->right = createNode(50);

root->left->left = createNode(25);

root->left->right = createNode(35);

root->left->left->left = createNode(15);

root->left->left->right = createNode(28);

root->right->left = createNode(45);

root->right->right = createNode(60);

root->right->right->left = createNode(55);

root->right->right->right = createNode(70);

printf("\n The Preorder traversal of given binary tree is -\n");

traversePreorder(root);

return 0;
}

18. Traverse a binary search tree in inorder.

#include <stdio.h>

#include <stdlib.h>

struct node {

int element;

struct node* left;

struct node* right;

};

struct node* createNode(int val)

struct node* Node = (struct node*)malloc(sizeof(struct node));

Node->element = val;

Node->left = NULL;

Node->right = NULL;

return (Node);

void traverseInorder(struct node* root)

if (root == NULL)

return;

traverseInorder(root->left);

printf(" %d ", root->element);

traverseInorder(root->right);

int main()

struct node* root = createNode(40);

root->left = createNode(30);

root->right = createNode(50);

root->left->left = createNode(25);
root->left->right = createNode(35);

root->left->left->left = createNode(15);

root->left->left->right = createNode(28);

root->right->left = createNode(45);

root->right->right = createNode(60);

root->right->right->left = createNode(55);

root->right->right->right = createNode(70);

printf("\n The Inorder traversal of given binary tree is -\n");

traverseInorder(root);

return 0;

19. Traverse a binary search tree in postorder.

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *left, *right;

};

struct node *newNode (int item)

struct node *temporary = (struct node *) malloc (sizeof (struct node));

temporary->data = item;

temporary->left = temporary->right = NULL;

return temporary;

}
void postorder (struct node *root)

if (root != NULL)

postorder (root->left);

postorder (root->right);

printf ("%d ", root->data);

struct node *insert (struct node *node, int data)

if (node == NULL)

return newNode (data);

if (data < node->data)

node->left = insert (node->left, data);

else if (data > node->data)

node->right = insert (node->right, data);

return node;

int main ()

struct node *root = NULL;


root = insert (root, 9);

insert (root, 7);

insert (root, 5);

insert (root, 8);

insert (root, 14);

insert (root, 11);

insert (root, 16);

printf ("The postorder is :\n");

postorder (root);

return 0;

20. Implement exchange sort.

#include<stdio.h>

main()

float arr[100],temp;

int i,j,elements;

printf("Enter the number of elements in the array: ");

scanf("%d",&elements);

for(i = 0; i < elements; i++)

printf("Arr[%d}=",i);

scanf("%f",&arr[i]);

printf("\nExisting list\n");

for(i = 0; i < elements; i++)

printf("arr[%d]=%g\n",i,arr[i]);

for(i = 0; i < elements-1; i++)


for(j = 0; j < elements-i-1; j++)

if(arr[j]>arr[j+1])

temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

printf("\nSorted List\n");

for(i = 0; i < elements; i++)

printf("arr[%d]=%g\n",i,arr[i]);

getch();

21. Implement selection sort.

#include<stdio.h>

main()

float arr[100],temp;

int i,j,elements,min;

printf("Enter the number of elements in the array: ");

scanf("%d",&elements);

for(i = 0; i < elements; i++)

printf("Arr[%d}=",i);

scanf("%f",&arr[i]);

printf("\nExisting list\n");

for(i = 0; i < elements; i++)

printf("arr[%d]=%g\n",i,arr[i]);

for(i = 0; i < elements-1; i++)

{
min=i;

for(j=i+1;j<elements;j++)

if(arr[j]<arr[min])

min=j;

temp=arr[i];

arr[i]=arr[min];

arr[min]=temp;

printf("\nSorted List\n");

for(i = 0; i < elements; i++)

printf("arr[%d]=%g\n",i,arr[i]);

getch();

22. Implement insertion sort.

#include<stdio.h>

main()

float arr[100],temp;

int i,j,elements;

printf("Enter the number of elements in the array: ");

scanf("%d",&elements);

for(i = 0; i < elements; i++)

printf("Arr[%d}=",i);

scanf("%f",&arr[i]);

printf("\nExisting list\n");

for(i = 0; i < elements; i++)

printf("arr[%d]=%g\n",i,arr[i]);

for(i = 1; i < elements; i++)


{

temp=arr[i];

for(j=i-1;((j>=0)&&(arr[j]>temp));j--)

{arr[j+1]=arr[j];

arr[j+1]=temp;

printf("\nThe Sorted List\n");

for(i=0;i<elements;i++)

printf("arr[%d]=%g\n",i,arr[i]);

getch();

23. Implement quick sort.

#include<stdio.h>

main()

void q_sort(float [],int,int);

float arr[100];

int i,elements;

printf("Enter the number of elements in the array: ");

scanf("%d",&elements);

for(i = 0; i < elements; i++)

printf("Arr[%d}=",i);

scanf("%f",&arr[i]);

printf("\nExisting list\n");

for(i = 0; i < elements; i++)

printf("arr[%d]=%g\n",i,arr[i]);

q_sort(arr,0,elements-1);
printf("\nThe Sorted List\n");

for(i=0;i<elements;i++)

printf("arr[%d]=%g\n",i,arr[i]);

getch();

void q_sort(float a[],int lower,int upper)

int split(float a[],int,int);

int v;

if(upper>lower)

v=split(a,lower,upper);

q_sort(a,lower,v-1);

q_sort(a,v+1,upper);

int split(float a[],int lr,int ur)

int v,p,q,t;

p=lr+1;

q=ur;

v=a[lr];

while(q>=p)

while(a[p]<v)p++;

while(a[q]>v)q--;

if(q>p)

t=a[p];a[p]=a[q];a[q]=t;

}
t=a[lr];

a[lr]=a[q];

a[q]=t;

return q;

You might also like