quicksort java example

Example 1: quick sort code in java

import java.util.*;
class QuickSort {
    //selects last element as pivot, pi using which array is partitioned.
    int partition(int intArray[], int low, int high) {
        int pi = intArray[high];
        int i = (low-1); // smaller element index
        for (int j=low; j<high; j++) {
            // check if current element is less than or equal to pi
            if (intArray[j] <= pi) {
                i++;
                // swap intArray[i] and intArray[j]
                int temp = intArray[i];
                intArray[i] = intArray[j];
                intArray[j] = temp;
            }
        }

        // swap intArray[i+1] and intArray[high] (or pi)
        int temp = intArray[i+1];
        intArray[i+1] = intArray[high];
        intArray[high] = temp;

        return i+1;
    }


    //routine to sort the array partitions recursively
    void quick_sort(int intArray[], int low, int high) {
        if (low < high) {
            //partition the array around pi=>partitioning index and return pi
            int pi = partition(intArray, low, high);

            // sort each partition recursively
            quick_sort(intArray, low, pi-1);
            quick_sort(intArray, pi+1, high);
        }
    }
}

class QUICK_SORT{
    public static void main(String args[]) {
        //initialize a numeric array, intArray
        int intArray[] = {3,2,1,6,5,4};
        int n = intArray.length;
        //print the original array
        System.out.println("Original Array: " + Arrays.toString(intArray));
        //call quick_sort routine using QuickSort object
        QuickSort obj = new QuickSort();
        obj.quick_sort(intArray, 0, n-1);
        //print the sorted array
        System.out.println("\nSorted Array: " + Arrays.toString(intArray));
    }
}

Example 2: quicksort in code

// A full c++ quicksort algorithm no bs
// quicksort in code

#include <iostream>

using namespace std;

void QuickSort(int arr[], int start, int end);
int Partition(int arr[], int start, int end);
void SwapArrMem(int arr[], int a, int b);

int main()
{

	int arr[4]; //change the size of the array to your desired array size

	cout << "enter " << sizeof(arr) / sizeof(arr[0]) << " numbers. press enter after input" << endl;

	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		
		cin >> arr[i];
	}

	cout << endl << "The sorted numbers are:" << endl << endl;



	QuickSort(arr, 0, sizeof(arr) / sizeof(arr[0]) - 1);

	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		cout << arr[i] << endl;
	}

}

void QuickSort(int arr[], int start, int end)
{
	if (start >= end) return;

	int index = Partition(arr, start, end);
	QuickSort(arr, start, index - 1);
	QuickSort(arr, index + 1, end);
}

int Partition(int arr[], int start, int end)
{
	int pivotindex = start;
	int pivotvalue = arr[end];
	for (int i = start; i < end; i++)
	{
		if (arr[i] < pivotvalue)
		{
			SwapArrMem(arr, i, pivotindex);
			pivotindex++;
		}
	}
	SwapArrMem(arr, pivotindex, end);
	return pivotindex;
}

void SwapArrMem(int arr[], int a, int b)
{
	int temp = arr[a];
	arr[a] = arr[b];
	arr[b] = temp;
}

Tags:

Cpp Example