0% found this document useful (0 votes)
4 views5 pages

daa2

The document discusses Quick Sort and Randomized Quick Sort algorithms, detailing their theory, pseudo code, and time complexity analysis. Quick Sort operates by selecting a pivot and partitioning the array, while Randomized Quick Sort enhances this by randomly selecting the pivot to avoid worst-case scenarios. Both algorithms have an average time complexity of O(n log n), with Randomized Quick Sort being preferred for its robustness in performance.

Uploaded by

ispande.ekta23
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)
4 views5 pages

daa2

The document discusses Quick Sort and Randomized Quick Sort algorithms, detailing their theory, pseudo code, and time complexity analysis. Quick Sort operates by selecting a pivot and partitioning the array, while Randomized Quick Sort enhances this by randomly selecting the pivot to avoid worst-case scenarios. Both algorithms have an average time complexity of O(n log n), with Randomized Quick Sort being preferred for its robustness in performance.

Uploaded by

ispande.ekta23
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/ 5

Bansilal Ramnath Agarwal Charitable Trust’s

Vishwakarma Institute of Technology, Pune-37


ET3272: Design and Analysis of Algorithm
(An Autonomous Institute Affiliated to Savitribai Pune University)

Department of Electronics & Telecommunication Engineering


Name of the student: Ekta Ispande Roll No. 69
Div:B Batch:3
Date of performance:

Experiment no. :- 02

Title: Quick and Randomized Quick Sort

1. Theory :

Quick Sort is a highly efficient sorting algorithm based on the divide-and-conquer paradigm. It works
by selecting a pivot element from the array and partitioning the other elements into two sub-arrays: one
with elements less than the pivot and the other with elements greater than the pivot. The process is then
repeated recursively for the sub-arrays.

Randomized Quick Sort is a variation of Quick Sort where the pivot is chosen randomly. This
randomization helps to avoid the worst-case scenario (O(n²)) that occurs in Quick Sort when the pivot is
consistently the smallest or largest element in the array. On average, both algorithms have a time
complexity of O(n log n).
The problem statement involves implementing and analyzing both Quick Sort and Randomized Quick
Sort to compare their performance in terms of time complexity and practical execution time.

2. Algorithm in Pseudo Code format :

a) Quick Sort:

Pseudo Code :

QUICKSORT(A, low, high):


if low < high:
pivot_index = PARTITION(A, low, high)
QUICKSORT(A, low, pivot_index - 1)
QUICKSORT(A, pivot_index + 1, high)

PARTITION(A, low, high):


pivot = A[high]
i = low - 1
for j = low to high - 1:
if A[j] <= pivot:
i = i + 1
SWAP(A[i], A[j])
SWAP(A[i + 1], A[high])
return i + 1
b) Randomized Quick Sort:

Pseudo Code :

RANDOMIZED_QUICKSORT(A, low, high):


if low < high:
pivot_index = RANDOMIZED_PARTITION(A, low, high)
RANDOMIZED_QUICKSORT(A, low, pivot_index - 1)
RANDOMIZED_QUICKSORT(A, pivot_index + 1, high)

RANDOMIZED_PARTITION(A, low, high):


random_index = RANDOM(low, high)
SWAP(A[random_index], A[high])
return PARTITION(A, low, high)

3. Analysis of the Algorithm

Time Complexity:
 Best Case: O(n log n) - Occurs when the pivot divides the array into two nearly equal parts.
 Average Case: O(n log n) - Expected case for Randomized Quick Sort.
 Worst Case: O(n²) - Occurs in Quick Sort when the pivot is consistently the smallest or largest
element. Randomized Quick Sort mitigates this by randomizing pivot selection.
Space Complexity:
 O(log n) - Due to the recursive call stack.

4. Experiment and Result

a. Quick Sort Code:

import java.util.Scanner;

public class QuickSort {

// Quick Sort function


public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high); // Partition the
array
quickSort(arr, low, pivotIndex - 1); // Sort the left subarray
quickSort(arr, pivotIndex + 1, high); // Sort the right subarray
}
}

// Partition function
private static int partition(int[] arr, int low, int high) {
int pivot = arr[high]; // Choose the last element as the pivot
int i = low - 1; // Index of the smaller element
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(arr, i, j); // Swap arr[i] and arr[j]
}
}
swap(arr, i + 1, high); // Swap arr[i+1] and arr[high] (pivot)
return i + 1;
}

// Utility function to swap two elements


private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Function to print the array
public static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}

// Main function for Quick Sort


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");


int n = scanner.nextInt();
int[] arr = new int[n];

System.out.println("Enter the elements of the array:");


for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

System.out.println("Original array:");
printArray(arr);

quickSort(arr, 0, n - 1);

System.out.println("Array sorted with Quick Sort:");


printArray(arr);

scanner.close();
}
}

Result:

b. Randomizied Quick Sort Code:

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

public class RandomizedQuickSort {

// Randomized Quick Sort function


public static void randomizedQuickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIndex = randomizedPartition(arr, low, high); //
Randomized partition
randomizedQuickSort(arr, low, pivotIndex - 1); // Sort the left
subarray
randomizedQuickSort(arr, pivotIndex + 1, high); // Sort the
right subarray
}
}

// Randomized Partition function


private static int randomizedPartition(int[] arr, int low, int high) {
Random rand = new Random();
int randomIndex = rand.nextInt(high - low + 1) + low; // Random
index between low and high
swap(arr, randomIndex, high); // Swap the random element with the
last element
return partition(arr, low, high); // Use the same partition function
}

// Partition function
private static int partition(int[] arr, int low, int high) {
int pivot = arr[high]; // Choose the last element as the pivot
int i = low - 1; // Index of the smaller element
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {

i++;
swap(arr, i, j); // Swap arr[i] and arr[j]
}
}
swap(arr, i + 1, high); // Swap arr[i+1] and arr[high] (pivot)
return i + 1;
}

// Utility function to swap two elements


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

// Function to print the array


public static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}

// Main function for Randomized Quick Sort


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");


int n = scanner.nextInt();
int[] arr = new int[n];

System.out.println("Enter the elements of the array:");


for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

System.out.println("Original array:");
printArray(arr);

randomizedQuickSort(arr, 0, n - 1);

System.out.println("Array sorted with Randomized Quick Sort:");


printArray(arr);

scanner.close();
}
}

Result:-
5. Conclusion:
Quick Sort and Randomized Quick Sort are both efficient sorting algorithms with an average time
complexity of O(n log n). While Quick Sort is simpler and faster in most cases, Randomized Quick Sort
provides a more robust solution by reducing the likelihood of worst-case performance. The choice
between the two depends on the specific use case and the nature of the input data. For general-purpose
sorting, Randomized Quick Sort is often preferred due to its consistent performance..

You might also like