0% found this document useful (0 votes)
8 views3 pages

Daa 05 01

design and analysis of algorithm

Uploaded by

Aashman Verma
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)
8 views3 pages

Daa 05 01

design and analysis of algorithm

Uploaded by

Aashman Verma
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
You are on page 1/ 3

DEPARTMENT OF

COMPUTER SCIENCE &

Experiment
Student Name: Ansh Jain UID: 22BCS15216
Branch: BE-CSE Section/Group: 603-B
th
Semester: 5 Date of Performance: 29/08/24
Subject Name: Design and analysis of algorithm
Subject Code: 22CSH-311

1. Aim – Sort a given set of elements using the Quick sort method and
determine the time required to sort the elements. Repeat the experiment
for different values of n, the number of elements in the list to be sorted.
The elements can be read from a file or can be generated using the random
number generator

2. Objective - To understand quick sort

3. Algorithm-

1. Select an element from the array as the pivot. Common choices are the
first element, the last element, the middle element, or a random element.
2. All elements less than the pivot are placed to its left.
3. All elements greater than the pivot are placed to its right.
4. Recursively apply the same process to the subarrays on the left and right
of the pivot.
5. If the subarray has one or zero elements, it is already sorted, and
recursion ends.
6. Combine the sorted subarrays with the pivot in between to form a sorted
array.

4. Code –
DEPARTMENT OF
COMPUTER SCIENCE &

public class QuickSort {


public static void quickSort(int[] array, int low, int high) {
if (low < high) {
int pivotIndex = partition(array, low, high);
quickSort(array, low, pivotIndex - 1);
quickSort(array, pivotIndex + 1, high);
}
}
public static int partition(int[] array, int low, int high) {
int pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] < pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return i + 1;
}
public static void main(String[] args) {
int[] array = {34, 7, 23, 32, 5, 62};

System.out.println("Original Array:");
for (int num : array) {
System.out.print(num + " ");
}
quickSort(array, 0, array.length - 1);
DEPARTMENT OF
COMPUTER SCIENCE &

System.out.println("\nSorted Array:");
for (int num : array) {
System.out.print(num + " ");
}
}
}

5. Output –

6. Learning Outcomes-
1. Understanding Quick sort Concepts
2. Concept of pivot element
3. Concept of recursion

7. Time complexity –
In worst case – O(n^2)
In best case – O(nlogn)

You might also like