How to write a c program to implement quick sort in C Programming Language ?
Solution:
/*Implement quick sort*/
#include <stdio.h>
#include <stdlib.h>
int quickSort(int *data, int left, int right) {
int pivot = left;
int i = left +1, j = right, temp;
if (left < right) {
while (1) {
while (data[j] > data[pivot])
j--;
while (data[i] <= data[pivot])
i++;
if (i < j) {
temp = data[i];
data[i] = data[j];
data[j] = temp;
} else
break;
}
temp = data[pivot];
data[pivot] = data[j];
data[j] = temp;
quickSort(data, left, j-1);
quickSort(data, j+1, right);
}
return 0;
}
int main() {
int i, j, num, *data;
printf("Enter ur number of entries:");
scanf("%d", &num);
data = (int *)malloc(sizeof (int) * num);
for (i = 0; i < num; i++) {
scanf("%d", &data[i]);
}
quickSort(data, 0, num-1);
printf("After sorting:\n");
for (i = 0; i < num; i++)
printf("%3d", data[i]);
printf("\n");
return;
}
Learn More :
Implementation
- C Program To Implement Heap Sort
- Implements an 8-bit sample and hold ADC on the MSP430
- C Program to Implements a dictionary's functionality.
- C Program to Implements a dictionary's functionality
- C Program to Implementation of List ADT as linked-list
- Pre Order, Post order, In order Implement Binary Tree using linked list
- Implementation of your "Fury of Dracula" Dracula AI
- C Program to Implement Dijkstra's Algorithm
- C Program to Implemention Bubble Sort using array
- C Program to Create, Display, Insert and Deletion of Queue Elements
- Linked List For Getting Employee Details, Display and Search For Salary C Program
Sort
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program Sort Example
- C Program Shell Sort Using C Programming Language
- C Program Selection Sort Using C Programming Language
- C Program to Bubble Sort Using C Programming Language
- C Program Insertion Sort Using C Programming Language
- Heap Sort Using C Programming Language
- C Program To Sort An Array In Ascending And Descending Order
- C Program To Sort An Array Of Names In Alphabetical And Reverse Order
- C Program Sort Array By Segment
- C Program to sort an array using bubble sort
- C Program to merge and sort two arrays
- Un-sortiertes Array and Sortiertes Array
- matrix sort in C Program
- C program that receives 10 float numbers from the console and sort them in non-ascending order, and prints the result
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- C Program to accept n numbers from user,store these numbers into an array & sort the number of an array
- QuickVSInsertion Sorting C Program