DAA Lab Assignment 2
DAA Lab Assignment 2
ROLL NO: 10
CLASS: IT-C
PRN NO: 12211772
BATCH: 2
LAB ASSIGNMENT 2
STEP1: Pseudocode
QUICKSORT(arr, si, ei):
IF si >= ei:
RETURN // Base case: Stop when there's one or no element
FOR j = si TO ei - 1:
IF arr[j] <= pivot:
i = i + 1 // Move pointer
SWAP(arr[i], arr[j]) // Swap elements
i++
SWAP(arr[i ], arr[ei]) // Place pivot in correct position
RETURN i // Return pivot index
STEP 2: Code
import java.util.Arrays;
public class Main {
// QuickSort function
public static void quicksort(int arr[], int si, int ei) {
if (si >= ei) return; // Base case
for (int j = si; j < ei; j++) { // Iterate through the array
if (arr[j] <= pivot) { // If element is smaller or equal to pivot
i++; // Move the pointer forward
// Swap arr[i] and arr[j]
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
i++;
// Swap pivot to correct position
int temp = arr[ei];
arr[ei] = arr[i];
arr[i] = temp;
70k 20
80k 23
Output :