Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Quick Sorting algorithm. #2815

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Java/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.util.Arrays;

public class MergeSort {
public static void mergeSort(int[] array, int left, int right) {
if (left < right) {
int middle = (left + right) / 2;

// Sort first and second halves
mergeSort(array, left, middle);
mergeSort(array, middle + 1, right);

// Merge the sorted halves
merge(array, left, middle, right);
}
}

// Merging two subarrays of array[]
public static void merge(int[] array, int left, int middle, int right) {
int n1 = middle - left + 1;
int n2 = right - middle;

int[] leftArray = new int[n1];
int[] rightArray = new int[n2];

System.arraycopy(array, left, leftArray, 0, n1);
System.arraycopy(array, middle + 1, rightArray, 0, n2);

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
array[k] = leftArray[i];
i++;
} else {
array[k] = rightArray[j];
j++;
}
k++;
}

while (i < n1) {
array[k] = leftArray[i];
i++;
k++;
}

while (j < n2) {
array[k] = rightArray[j];
j++;
k++;
}
}

public static void main(String[] args) {
int[] array = { 12, 11, 13, 5, 6, 7 };
System.out.println("Original array: " + Arrays.toString(array));

mergeSort(array, 0, array.length - 1);

System.out.println("Sorted array: " + Arrays.toString(array));
}
}
55 changes: 55 additions & 0 deletions Java/Quicksort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.Arrays;

public class QuickSort {

// Function to perform the quicksort
public static void quicksort(int[] array, int low, int high) {
if (low < high) {
// Partition the array and get the pivot index
int pivotIndex = partition(array, low, high);

// Recursively apply quicksort to the left and right partitions
quicksort(array, low, pivotIndex - 1);
quicksort(array, pivotIndex + 1, high);
}
}

// Function to partition the array
private static int partition(int[] array, int low, int high) {
// Choose the last element as the pivot
int pivot = array[high];
int i = low - 1; // Pointer for greater element

// Traverse through all elements
for (int j = low; j < high; j++) {
// If current element is smaller than the pivot
if (array[j] < pivot) {
i++;
// Swap element at i with element at j
swap(array, i, j);
}
}

// Swap the pivot element with the element at i + 1
swap(array, i + 1, high);
return i + 1; // Return the pivot index
}

// Function to swap two elements
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}

// Main method to test the quicksort algorithm
public static void main(String[] args) {
int[] array = { 34, 7, 23, 32, 5, 62, 14 };
System.out.println("Original array: " + Arrays.toString(array));

// Apply quicksort
quicksort(array, 0, array.length - 1);

System.out.println("Sorted array: " + Arrays.toString(array));
}
}
Binary file added QuickSort.class
Binary file not shown.