Skip to content

kdilakshi/Quick_Sort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Quick_Sort

.java class QuickSort { // Partition method to place pivot at correct position public static int partition(int arr[], int low, int high) { int pivot = arr[high]; // Choosing the last element as pivot int i = low - 1; // Pointer for smaller elements

    for (int j = low; j < high; j++) {
        if (arr[j] < pivot) { 
            i++;
            // Swap arr[i] and arr[j]
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    // Swap pivot with element at (i+1) position
    int temp = arr[i + 1];
    arr[i + 1] = arr[high];
    arr[high] = temp;

    return i + 1; // New pivot index
}

// QuickSort recursive method
public static void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pivotIndex = partition(arr, low, high); // Find pivot position
        quickSort(arr, low, pivotIndex - 1); // Sort left partition
        quickSort(arr, pivotIndex + 1, high); // Sort right partition
    }
}

// Main method to test Quick Sort
public static void main(String args[]) {
    int arr[] = {10, 7, 8, 9, 1, 5};
    int n = arr.length;

    System.out.println("Unsorted Array:");
    for (int num : arr) {
        System.out.print(num + " ");
    }

    quickSort(arr, 0, n - 1); // Call Quick Sort

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

}

About

,java

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors