0% found this document useful (0 votes)
3 views2 pages

Quick Sort Algorithm in C for Integers

The document provides a C program that implements the Quick Sort algorithm to sort a list of integers in ascending order. It includes functions for swapping elements and recursively sorting the array based on a chosen pivot. The program prompts the user to enter the number of elements and their values, then outputs the sorted list.

Uploaded by

shafikhan9258
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)
3 views2 pages

Quick Sort Algorithm in C for Integers

The document provides a C program that implements the Quick Sort algorithm to sort a list of integers in ascending order. It includes functions for swapping elements and recursively sorting the array based on a chosen pivot. The program prompts the user to enter the number of elements and their values, then outputs the sorted list.

Uploaded by

shafikhan9258
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

Write a program that implements Quick sort sorting methods to sort a

given list of integers in ascending order

#include <stdio.h>

// Function to swap two elements


void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}

void quicksort(int number[25],int first,int last)


{
int i, j, pivot, temp;
if(first<last)
{
pivot=first; // Choose the first element as pivot
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j) // swap two elements
{
swap(&number[i], &number[j]);
}
}
// Swap the pivot element with the element at i+1
position
swap(&number[pivot], &number[j]);
// Recursive call on the left of pivot
quicksort(number,first,j-1);
// Recursive call on the right of pivot
quicksort(number,j+1,last);
}
}
int main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
Write a program that implements Quick sort sorting methods to sort a
given list of integers in ascending order

for(i=0;i<count;i++)
{
printf("\nEnter %d element: ", i+1);
scanf("%d",&number[i]);
}
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}

OUTPUT
How many elements are u going to enter?: 10
Enter 1 element: 3
Enter 2 element: 6
Enter 3 element: 9
Enter 4 element: 8
Enter 5 element: 5
Enter 6 element: 2
Enter 7 element: 1
Enter 8 element: 4
Enter 9 element: 7
Enter 10 element: 10
Order of Sorted elements: 1 2 3 4 5 6 7 8 9 10

You might also like