0% found this document useful (0 votes)
114 views

DS Unit 1 Lab Programs

The document describes three experiments involving linear linked lists and arrays: 1) A menu-driven program implementing insert, delete, find, and display operations on an array; 2) Using linear and binary search to find an element in an array; 3) A menu-driven program to insert, delete, search, and display elements in a linked list stored in ascending order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

DS Unit 1 Lab Programs

The document describes three experiments involving linear linked lists and arrays: 1) A menu-driven program implementing insert, delete, find, and display operations on an array; 2) Using linear and binary search to find an element in an array; 3) A menu-driven program to insert, delete, search, and display elements in a linked list stored in ascending order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Experiment-1

Write a menu driven program that implement following operations (using separate

functions) on a linear array:

a) Insert a new element at end as well as at a given position.

b) Delete an element from a given whose value is given or whose position is given.

c) To find the location of a given element.

d) To display the elements of the linear array.

Program in C
#include<stdio.h>

#include<stdlib.h>

#define MAX 10

int a[MAX], pos, elem;

int n = 0;

void create();

void display();

void insert();

void delete();

void findlocation();

void main()

int choice;
while(1)

printf("\n\n~~~~MENU~~~~");

printf("\n=>1. Create an array of N integers");

printf("\n=>2. Display of array elements");

printf("\n=>3. Insert ELEM at a given POS");

printf("\n=>4. Delete an element at a given POS");

printf("\n=>5. Find the Location of a given Element");

printf("\n=>6. Exit");

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch(choice)

case 1: create();

break;

case 2: display();

break;

case 3: insert();

break;

case 4: delete();

break;

case 5: findlocation();

break;

case 6: exit(1);

break;
default: printf("\nPlease enter a valid choice:");

void create()

int i;

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

scanf("%d", &n);

printf("\nEnter the elements: ");

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

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

void display()

int i;

if(n == 0)

printf("\nNo elements to display");

return;

}
printf("\nArray elements are: ");

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

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

void insert()

int i;

if(n == MAX)

printf("\nArray is full. Insertion is not possible");

return;

do

printf("\nEnter a valid position where element to be inserted: ");

scanf("%d", &pos);

}while(pos > n);

printf("\nEnter the value to be inserted: ");

scanf("%d", &elem);

for(i=n-1; i>=pos ; i--)


{

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

a[pos] = elem;

n = n+1;

display();

void delete()

int i;

if(n == 0)

printf("\nArray is empty and no elements to delete");

return;

do

printf("\nEnter a valid position from where element to be deleted: ");

scanf("%d", &pos);

}while(pos>=n);

elem = a[pos];
printf("\nDeleted element is : %d \n", elem);

for( i = pos; i< n-1; i++)

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

n = n-1;

display();

void findlocation()

int element = 0;int position = 0 ; int i ;

printf("Enter the element whose position is to be found: ");

scanf("%d", &element);

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

if(a[i] == element)

position = i+1;

break;

} //display();

printf("\n The position of %d in array is: %d", element, position);

printf("\n The Index location of %d in array is: %d", element, i);}


OUTPUT

~~~~MENU~~~~

=>1. Create an array of N integers

=>2. Display of array elements

=>3. Insert ELEM at a given POS

=>4. Delete an element at a given POS

=>5. Find the Location of a given Element

=>6. Exit

Enter your choice: 1

Enter the number of elements: 4

Enter the elements: 2

~~~~MENU~~~~

=>1. Create an array of N integers

=>2. Display of array elements

=>3. Insert ELEM at a given POS

=>4. Delete an element at a given POS

=>5. Find the Location of a given Element

=>6. Exit
Enter your choice: 2

Array elements are: 2 3 4 5

~~~~MENU~~~~

=>1. Create an array of N integers

=>2. Display of array elements

=>3. Insert ELEM at a given POS

=>4. Delete an element at a given POS

=>5. Find the Location of a given Element

=>6. Exit

Enter your choice: 3

Enter a valid position where element to be inserted: 2

Enter the value to be inserted: 6

Array elements are: 2 3 6 4 5

~~~~MENU~~~~

=>1. Create an array of N integers

=>2. Display of array elements

=>3. Insert ELEM at a given POS

=>4. Delete an element at a given POS

=>5. Find the Location of a given Element


=>6. Exit

Enter your choice: 2

Array elements are: 2 3 6 4 5

~~~~MENU~~~~

=>1. Create an array of N integers

=>2. Display of array elements

=>3. Insert ELEM at a given POS

=>4. Delete an element at a given POS

=>5. Find the Location of a given Element

=>6. Exit

Enter your choice: 4

Enter a valid position from where element to be deleted: 1

Deleted element is : 3

Array elements are: 2 6 4 5

~~~~MENU~~~~

=>1. Create an array of N integers

=>2. Display of array elements

=>3. Insert ELEM at a given POS

=>4. Delete an element at a given POS


=>5. Find the Location of a given Element

=>6. Exit

Enter your choice: 5

Enter the element whose position is to be found: 4

The position of 4 in array is: 3

The Index location of 4 in array is: 2

~~~~MENU~~~~

=>1. Create an array of N integers

=>2. Display of array elements

=>3. Insert ELEM at a given POS

=>4. Delete an element at a given POS

=>5. Find the Location of a given Element

=>6. Exit

Enter your choice:


Experiment-2

Write a program to demonstrate the use of linear and binary search to find a given element
in an array.

To search an element in a given array, there are two popular algorithms available:

1. Linear Search

2. Binary Search

Linear Search
#include<stdio.h>

#include<conio.h>

void main()

int arr[30];

int i, size, search;

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

printf("Enter total no. of elements : ");

scanf("%d",&size);

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

printf("Enter %d element : ",i+1);

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

printf("Enter the element to be searched:");

scanf("%d",&search);

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

{
if(search==arr[i]) // if both search element value is matched with the

// array value

printf("\n Element exits in the list at position : %d",i+1);

printf("\n Element exits in the list at index location : %d",i);

break;

}getch();

Output:

-- Linear Search --

Enter total no. of elements : 4

Enter 1 element : 6

Enter 2 element : 8
Enter 3 element : 2

Enter 4 element : 5

Enter the element to be searched:2

Element exits in the list at position : 3

Element exits in the list at index location : 2


...Program finished with exit code 0

Press ENTER to exit console.

Binary Search

#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements");
scanf("%d",&n);
printf("Enter %d integers", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at the position %d ", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list", key);
return 0;
}
Output:

Enter number of elements 4


Enter 4 integers 6
7
8
9
Enter value to find
8
8 found at the position 3

Enter number of elements 3

Enter 3 integers 2 4 7

Enter value to find 8

Not found! 8 isn't present in the list


Experiment-3 Write a menu driven program that maintains a linear linked
list whose elements are stored in on ascending order and implements the
following operations (using separate functions):
a) Insert a new element
b) Delete an existing element
c) Search an element
d) Display all the elements

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert ();


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");

printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at the desired
location\n4.Delete from Beginning\n5.Delete from last\n6.Delete node after
specified location\n7.Search for an element\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("\n Please enter valid choice..\n");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}

}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted\n");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted\n");

}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert\n");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}

}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}

else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform
deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;

if(ptr == NULL)
{
printf("\nCan't delete\n");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("\nNothing to print\n");
}
else
{
printf("\n The Linked List is . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
} }
Output
*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at the desired location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter value

Node inserted
*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at the desired location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter value

Node inserted
*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at the desired location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter value

Node inserted
*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at the desired location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter value?

Node inserted
*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at the desired location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

The Linked List is…….

3
*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at the desired location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

C++ Program

/*

* C++ Program to Implement Singly Linked List


*/

#include<iostream>

#include<cstdio>

#include<cstdlib>

using namespace std;

/*

* Node Declaration

*/

struct node

int info;

struct node *next;

}*start;

/*

* Class Declaration

*/

class single_llist

public:
node* create_node(int);

void insert_begin();

void insert_pos();

void insert_last();

void delete_pos();

void sort();

void search();

void update();

void reverse();

void display();

single_llist()

start = NULL;

};

/*

* Main :contains menu

*/

main()
{

int choice, nodes, element, position, i;

single_llist sl;

start = NULL;

while (1)

cout<<endl<<"---------------------------------"<<endl;

cout<<endl<<"Operations on singly linked list"<<endl;

cout<<endl<<"---------------------------------"<<endl;

cout<<"1.Insert Node at beginning"<<endl;

cout<<"2.Insert node at last"<<endl;

cout<<"3.Insert node at position"<<endl;

cout<<"4.Sort Link List"<<endl;

cout<<"5.Delete a Particular Node"<<endl;

cout<<"6.Update Node Value"<<endl;

cout<<"7.Search Element"<<endl;

cout<<"8.Display Linked List"<<endl;

cout<<"9.Reverse Linked List "<<endl;

cout<<"10.Exit "<<endl;

cout<<"Enter your choice : ";


cin>>choice;

switch(choice)

case 1:

cout<<"Inserting Node at Beginning: "<<endl;

sl.insert_begin();

cout<<endl;

break;

case 2:

cout<<"Inserting Node at Last: "<<endl;

sl.insert_last();

cout<<endl;

break;

case 3:

cout<<"Inserting Node at a given position:"<<endl;

sl.insert_pos();

cout<<endl;

break;

case 4:

cout<<"Sort Link List: "<<endl;


sl.sort();

cout<<endl;

break;

case 5:

cout<<"Delete a particular node: "<<endl;

sl.delete_pos();

break;

case 6:

cout<<"Update Node Value:"<<endl;

sl.update();

cout<<endl;

break;

case 7:

cout<<"Search element in Link List: "<<endl;

sl.search();

cout<<endl;

break;

case 8:

cout<<"Display elements of link list"<<endl;

sl.display();
cout<<endl;

break;

case 9:

cout<<"Reverse elements of Link List"<<endl;

sl.reverse();

cout<<endl;

break;

case 10:

cout<<"Exiting..."<<endl;

exit(1);

break;

default:

cout<<"Wrong choice"<<endl;

/*

* Creating Node

*/
node *single_llist::create_node(int value)

struct node *temp, *s;

temp = new(struct node);

if (temp == NULL)

cout<<"Memory not allocated "<<endl;

return 0;

else

temp->info = value;

temp->next = NULL;

return temp;

/*

* Inserting element in beginning

*/
void single_llist::insert_begin()

int value;

cout<<"Enter the value to be inserted: ";

cin>>value;

struct node *temp, *p;

temp = create_node(value);

if (start == NULL)

start = temp;

start->next = NULL;

else

p = start;

start = temp;

start->next = p;

cout<<"Element Inserted at beginning"<<endl;

}
/*

* Inserting Node at last

*/

void single_llist::insert_last()

int value;

cout<<"Enter the value to be inserted: ";

cin>>value;

struct node *temp, *s;

temp = create_node(value);

s = start;

while (s->next != NULL)

s = s->next;

temp->next = NULL;

s->next = temp;

cout<<"Element Inserted at last"<<endl;

}
/*

* Insertion of node at a given position

*/

void single_llist::insert_pos()

int value, pos, counter = 0;

cout<<"Enter the value to be inserted: ";

cin>>value;

struct node *temp, *s, *ptr;

temp = create_node(value);

cout<<"Enter the postion at which node to be inserted: ";

cin>>pos;

int i;

s = start;

while (s != NULL)

s = s->next;

counter++;

}
if (pos == 1)

if (start == NULL)

start = temp;

start->next = NULL;

else

ptr = start;

start = temp;

start->next = ptr;

else if (pos > 1 && pos <= counter)

s = start;

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

ptr = s;
s = s->next;

ptr->next = temp;

temp->next = s;

else

cout<<"Positon out of range"<<endl;

/*

* Sorting Link List

*/

void single_llist::sort()

struct node *ptr, *s;

int value;

if (start == NULL)

{
cout<<"The List is empty"<<endl;

return;

ptr = start;

while (ptr != NULL)

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

if (ptr->info > s->info)

value = ptr->info;

ptr->info = s->info;

s->info = value;

ptr = ptr->next;

/*
* Delete element at a given position

*/

void single_llist::delete_pos()

int pos, i, counter = 0;

if (start == NULL)

cout<<"List is empty"<<endl;

return;

cout<<"Enter the position of value to be deleted: ";

cin>>pos;

struct node *s, *ptr;

s = start;

if (pos == 1)

start = s->next;

else

{
while (s != NULL)

s = s->next;

counter++;

if (pos > 0 && pos <= counter)

s = start;

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

ptr = s;

s = s->next;

ptr->next = s->next;

else

cout<<"Position out of range"<<endl;

free(s);
cout<<"Element Deleted"<<endl;

/*

* Update a given Node

*/

void single_llist::update()

int value, pos, i;

if (start == NULL)

cout<<"List is empty"<<endl;

return;

cout<<"Enter the node postion to be updated: ";

cin>>pos;

cout<<"Enter the new value: ";

cin>>value;

struct node *s, *ptr;


s = start;

if (pos == 1)

start->info = value;

else

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

if (s == NULL)

cout<<"There are less than "<<pos<<" elements";

return;

s = s->next;

s->info = value;

cout<<"Node Updated"<<endl;

}
/*

* Searching an element

*/

void single_llist::search()

int value, pos = 0;

bool flag = false;

if (start == NULL)

cout<<"List is empty"<<endl;

return;

cout<<"Enter the value to be searched: ";

cin>>value;

struct node *s;

s = start;

while (s != NULL)

pos++;
if (s->info == value)

flag = true;

cout<<"Element "<<value<<" is found at position


"<<pos<<endl;

s = s->next;

if (!flag)

cout<<"Element "<<value<<" not found in the


list"<<endl;

/*

* Reverse Link List

*/

void single_llist::reverse()

struct node *ptr1, *ptr2, *ptr3;

if (start == NULL)

{
cout<<"List is empty"<<endl;

return;

if (start->next == NULL)

return;

ptr1 = start;

ptr2 = ptr1->next;

ptr3 = ptr2->next;

ptr1->next = NULL;

ptr2->next = ptr1;

while (ptr3 != NULL)

ptr1 = ptr2;

ptr2 = ptr3;

ptr3 = ptr3->next;

ptr2->next = ptr1;

start = ptr2;
}

/*

* Display Elements of a link list

*/

void single_llist::display()

struct node *temp;

if (start == NULL)

cout<<"The List is Empty"<<endl;

return;

temp = start;

cout<<"Elements of list are: "<<endl;

while (temp != NULL)

cout<<temp->info<<"->";

temp = temp->next;

}
cout<<"NULL"<<endl;

Experiment-4

Write a program to sort an array of integers in ascending/descending


order using

a) Bubble Sort.

b) Quick Sort

Bubble Sort:

#include <stdio.h>

void bubbleSort(int arr[], int n)


{
int i, j, temp, flag=0;
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
// introducing a flag to monitor swapping
if( arr[j] > arr[j+1])
{
// swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
// if swapping happens update flag to 1
flag = 1;
}
}
// if value of flag is zero after all the iterations of inner loop
// then break out
if(flag==0)
{
break;
}
}

// print the sorted array


printf("Sorted Array is: ");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}

int main()
{
int arr[100], i, n, step, temp;
// ask user for number of elements to be sorted
printf("Enter the number of elements to be sorted: ");
scanf("%d", &n);
// input elements if the array
for(i = 0; i < n; i++)
{
printf("Enter element no. %d: ", i+1);
scanf("%d", &arr[i]);
}
// call the function bubbleSort
bubbleSort(arr, n);
return 0; }
Output:

Enter the number of elements to be sorted: 4


Enter element no. 1: 2
Enter element no. 2: 7
Enter element no. 3: 3
Enter element no. 4: 1
Sorted Array is: 1 2 3 7

Quick Sort

#include <stdio.h>
int partition(int a[], int beg, int end);
void quickSort(int a[], int beg, int end);
void main()
{
int i;
int arr[11]={91,24,102,46,66,27,68,88,38,27,30};
quickSort(arr, 0, 10);
printf("\n The sorted array is: \n");
for(i=0;i<11;i++)
printf(" %d\t", arr[i]);
}
int partition(int a[], int beg, int end)
{

int left, right, temp, loc, flag;


loc = left = beg;
right = end;
flag = 0;
while(flag != 1)
{
while((a[loc] <= a[right]) && (loc!=right))
right--;
if(loc==right)
flag =1;
else if(a[loc]>a[right])
{
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if(flag!=1)
{
while((a[loc] >= a[left]) && (loc!=left))
left++;
if(loc==left)
flag =1;
else if(a[loc] <a[left])
{
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
return loc;
}
void quickSort(int a[], int beg, int end)
{

int loc;
if(beg<end)
{
loc = partition(a, beg, end);
quickSort(a, beg, loc-1);
quickSort(a, loc+1, end);
}
}
Output:
The sorted array is:
24 27 27 30 38 46 66 68 88 91 102
Program2
#include <stdio.h>

void quicksort (int [], int, int);

int main()

int list[60];

int size, i;

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

scanf("%d", &size);

printf("Enter the elements to be sorted:\n");

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

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

quicksort(list, 0, size - 1);


printf("After applying quick sort\n");

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

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

printf("\n");

return 0;

void quicksort(int list[], int low, int high)

int pivot, i, j, temp;

if (low < high)

pivot = low;

i = low;

j = high;

while (i < j)

while (list[i] <= list[pivot] && i <= high)

i++;

while (list[j] > list[pivot] && j >= low)

j--;
}

if (i < j)

temp = list[i];

list[i] = list[j];

list[j] = temp;

temp = list[j];

list[j] = list[pivot];

list[pivot] = temp;

quicksort(list, low, j - 1);

quicksort(list, j + 1, high);

Output:
Enter the number of elements: 5

Enter the elements to be sorted:

92

After applying quick sort

1 2 3 7 92

You might also like