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

Practical 3

This document contains a C program that implements the Quick Sort algorithm to sort an array of integers. It includes functions for swapping elements, partitioning the array, and printing the array. The main function prompts the user for input, sorts the array using Quick Sort, and displays the original and sorted arrays.

Uploaded by

ankurkumar99421
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)
14 views2 pages

Practical 3

This document contains a C program that implements the Quick Sort algorithm to sort an array of integers. It includes functions for swapping elements, partitioning the array, and printing the array. The main function prompts the user for input, sorts the array using Quick Sort, and displays the original and sorted arrays.

Uploaded by

ankurkumar99421
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/ 2

SOURCE CODE:

#include <stdio.h>

// Function to swap two elements

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

// Partition function to select pivot and arrange elements

int partition(int arr[], int low, int high) {

int pivot = arr[high]; // Taking the last element as the pivot

int i = low - 1; // Index for the smaller element

for (int j = low; j < high; j++) {

if (arr[j] < pivot) {

i++;

swap(&arr[i], &arr[j]); // Swap elements

swap(&arr[i + 1], &arr[high]); // Place pivot in correct position

return i + 1;

// Quick Sort function

void quickSort(int arr[], int low, int high) {

if (low < high) {

// Partition the array and get the pivot index

int pi = partition(arr, low, high);

// Recursively sort the two sub-arrays

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

}
// Function to print the array

void printArray(int arr[], int size) {

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

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

printf("\n");

int main() {

int n;

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

scanf("%d", &n);

int arr[n];

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

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

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

printf("Original array: \n");

printArray(arr, n);

quickSort(arr, 0, n - 1);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

OUTPUT :

You might also like