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

Program-02

The document outlines a program for sorting a set of integers using the Quick Sort algorithm, detailing its implementation in Java and analyzing its time complexity in worst, average, and best cases. It explains the divide-and-conquer approach of Quick Sort, including the steps for partitioning and recursive sorting. The program also includes a method to measure and display the time taken to sort varying sizes of arrays, with a focus on performance for n > 5000.

Uploaded by

raviba1998
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program-02

The document outlines a program for sorting a set of integers using the Quick Sort algorithm, detailing its implementation in Java and analyzing its time complexity in worst, average, and best cases. It explains the divide-and-conquer approach of Quick Sort, including the steps for partitioning and recursive sorting. The program also includes a method to measure and display the time taken to sort varying sizes of arrays, with a focus on performance for n > 5000.

Uploaded by

raviba1998
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Subject Code:21CS42

Subject :Design and Analysis of Algorithm

Program-02

2. Sort a given set of n integer elements using Quick Sort method and
compute its time complexity. Run the program for varied values of n> 5000
and record the time taken to sort. Plot a graph of the time taken versus n. The
elements can be read from a file or can be generated using the random
number generator. Demonstrate using C++/Java how the divide-and-conquer
method works along with its time complexity analysis: worst case, average
case and best case.

Theory Concept : Quick Sort

It is an algorithm of Divide & Conquer type.

Divide: Rearrange the elements and split arrays into two sub-arrays and an element
in between search that each element in left sub array is less than or equal to the
average element and each element in the right sub- array is larger than the middle
element.

Conquer: Recursively, sort two sub arrays.

Combine: Combine the already sorted array.

Search Creators… Page 1


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

ALGORITHM

1. QUICKSORT (array A, int m, int n)


2. 1 if (n > m)
3. 2 then
4. 3 i ← a random index from [m,n]
5. 4 swap A [i] with A[m]
6. 5 o ← PARTITION (A, m, n)
7. 6 QUICKSORT (A, m, o - 1)
8. 7 QUICKSORT (A, o + 1, n)

How Quick Sort works

1. Find a “pivot” item in the array. This item is the basis for
comparison for a single round.

2. Start a pointer (the left pointer) at the first item in the array.

3. Start a pointer (the right pointer) at the last item in the array.

4. While the value at the left pointer in the array is less than the
pivot value, move the left pointer to the right (add 1). Continue
until the value at the left pointer is greater than or equal to the
pivot value.

5. While the value at the right pointer in the array is greater than
the pivot value, move the right pointer to the left (subtract 1).
Continue until the value at the right pointer is less than or equal
to the pivot value.

Search Creators… Page 2


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

6. If the left pointer is less than or equal to the right pointer, then
swap the values at these locations in the array.

7. Move the left pointer to the right by one and the right pointer to
the left by one.

8. If the left pointer and right pointer don’t meet, go to step 1.

Search Creators… Page 3


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Example:

Search Creators… Page 4


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Time Complexities:

Worst Case Analysis: It is the case when items are already in sorted form and we try to sort
them again. This will takes lots of time and space.

Average Case: Generally, we assume the first element of the list as the pivot element.
In an average Case, the number of chances to get a pivot element is equal to the number
of items

Best Case: In any sorting, best case is the only case in which we don't make any comparison
between elements that is only done when we have only one element to sort.

PROGRAM

import java.util.Random;
import java.util.Scanner;

public class Quick_sort {


public static int SIZE = 7000;

public static void main(String[] args)throws


ArrayIndexOutOfBoundsException {
int a[] = new int[SIZE];
System.out.println("Enter Total number of Elements for sorting:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Random m = new Random();

Search Creators… Page 5


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

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


a[i] = m.nextInt(10)+1;
}
System.out.println("\n The Elements before sorting....");
for(int i=0;i<n;i++) {
System.out.println(""+a[i]);
}
long start_time,end_time;
start_time = System.nanoTime();
Quick(a, 0, n-1);
end_time = System.nanoTime();
System.out.println("\nThe Element after sorting");
for(int i=0;i<n;i++) {
System.out.println(""+a[i]);
}
System.out.println("\nThe time required for sorting "+n+"number
is:"+(end_time-start_time)+"ns");

}
static void Quick(int a[],int low,int high) {
int m,i;
if(low<high) {
m = Partition(a, low, high);
Quick(a, low, m-1);
Quick(a,m+1,high);
}
}
Search Creators… Page 6
21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

static int Partition(int a[], int low, int high) {


int pivot = a[low], i = low, j = high;
while (i<=j) {
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,low,j);
return j;

}
static void swap(int a[], int i, int j) {
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;

Search Creators… Page 7


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

OUTPUT

Search Creators… Page 8

You might also like