Daa 05 01
Daa 05 01
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
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 &
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)