You are on page 1of 9

Department of Electrical Engineering

Riphah International University, Islamabad, Pakistan

Program: B.Sc. Electrical Engineering Semester: III A


Subject: SEL-201 Data Structure & Algorithm Date: 02 DEC 2020

Experiment 6: Model quick sort algorithm by executing C programs.

OBJECTIVES:

To implement the quick sort algorithm in C language.

Name: Muhammad Noman Roll No: 12575

Performance Lab Report

Description Total Marks Description Total Marks


Marks Obtained Marks Obtained
Ability to conduct 5 Organization/Structure 5
Experiment &
Tool usage
Implementation 5 5
and Results Data Presentation
Total Marks obtained

Remarks (if any): ………………………………….

Name & Signature of faculty: …………………………………


Introduction
In case of quick sort algorithm we usually pick an element as a pivot and partitions the given
array around the picked pivot. There are many different versions of quick Sort that pick pivot in
different ways.
 Always pick first element as pivot.
 Always pick last element as pivot (implemented below)
 Pick a random element as pivot.
 Pick median as pivot.

Objectives
To learn the quick sort algorithm and implement in C programs.

Lab tasks

Lab task 01

Rearrange the following numbers using a Quicksort procedure. 42, 2, 8, 98,


76, 83, 98, 010, 76

Solution:
#include<stdio.h>

// A utility function to swap two elements


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

/* This function takes last element as pivot, places


the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
int j;

for (j = low; j <= high- 1; j++)


{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

/* The main function that implements QuickSort


arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);

// Separately sort elements before


// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

/* Function to print an array */


void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program to test above functions


int main()
{
int arr[] = {42, 2, 8, 98, 76, 83, 98, 10, 76};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
______________________________________________________________________________

Lab task 02

Apply the Quick sort on the following elements: 21,11,5,78,49, 54,72,88

Solution:

#include<stdio.h>

// A utility function to swap two elements


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

/* This function takes last element as pivot, places


the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
int j;

for (j = low; j <= high- 1; j++)


{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

/* The main function that implements QuickSort


arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);

// Separately sort elements before


// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program to test above functions


int main()
{
int arr[] = {21,11,5,78,49, 54,72,88};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

Lab task 03

Recursive Quicksort has no additional performance/implementation


advantages but can be used to understand recursion and sorting concepts
better.

Solution:
#include <stdio.h>
int main()
{
int Array[100], i, s, temp, Size;

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


scanf("%d", &Size);

printf(" Enter %d elements of an Array \n", Size);


for (i = 0; i < Size; i++)
{
scanf("%d", &Array[i]);
}
for (i = 0; i< Size; i++)
{
for (s = i + 1; s < Size; s++)
{
if(Array[i] < Array[s])
{
temp = Array[i];
Array[i] = Array[s];
Array[s] = temp;
}
}
}
printf("\n Array of Element in Descending Order are :\n");
for (i = 0; i < Size; i++)
{
printf("%d\t", Array[i]);
}
return 0;
}

Conclusion
After completing this lab tasks we are able to say that we can use this quick sort algorithm to
make different programs in future. From this sorting algorithm we can easily sort data
accordingly. It is very useful algorithm for sorting data.

You might also like