This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added quick & heap sorting Java code (#531)
- Loading branch information
1 parent
c84e7ff
commit 6cb8096
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package Sorting; | ||
|
||
/* | ||
Heap sort processes the elements by creating the min heap or max heap using | ||
the elements of the given array. Min heap or max heap represents the ordering | ||
of the array in which root element represents the minimum or maximum element | ||
of the array. At each step, the root element of the heap gets deleted & stored | ||
into the sorted array and the heap will again be heapified. | ||
The heap sort basically recursively performs two main operations. | ||
* Build a heap H, using the elements of ARR. | ||
* Repeatedly delete the root element of the heap formed in phase 1. | ||
*/ | ||
|
||
public class HeapSort { | ||
public void heapSort(int arr[]) | ||
{ | ||
int temp; | ||
//build the heap | ||
for (int i = arr.length / 2 - 1; i >= 0; i--) | ||
{ | ||
heapify(arr, arr.length, i); | ||
} | ||
//extract elements from the heap | ||
for (int i = arr.length - 1; i > 0; i--) | ||
{ | ||
//move current root to end (since it is the largest) | ||
temp = arr[0]; | ||
arr[0] = arr[i]; | ||
arr[i] = temp; | ||
//recall heapify to rebuild heap for the remaining elements | ||
heapify(arr, i, 0); | ||
} | ||
} | ||
|
||
void heapify(int arr[], int n, int i) | ||
{ | ||
int MAX = i; // Initialize largest as root | ||
int left = 2 * i + 1; //index of the left child of ith node = 2*i + 1 | ||
int right = 2 * i + 2; //index of the right child of ith node = 2*i + 2 | ||
int temp; | ||
|
||
//check if the left child of the root is larger than the root | ||
if (left < n && arr[left] > arr[MAX]) | ||
{ | ||
MAX = left; | ||
} | ||
//check if the right child of the root is larger than the root | ||
if (right < n && arr[right] > arr[MAX]) | ||
{ | ||
MAX = right; | ||
} | ||
//repeat the procedure for finding the largest element in the heap | ||
if (MAX != i) | ||
{ | ||
temp = arr[i]; | ||
arr[i] = arr[MAX]; | ||
arr[MAX] = temp; | ||
heapify(arr, n, MAX); | ||
} | ||
} | ||
|
||
//display the array | ||
void display(int arr[]) | ||
{ | ||
for (int i=0; i<arr.length; ++i) | ||
{ | ||
System.out.print(arr[i]+" "); | ||
} | ||
} | ||
|
||
public static void main(String args[]) | ||
{ | ||
int arr[] = { 12, 33, 52, 1, 12, 9 , 3, 10, 15 }; | ||
|
||
HeapSort sort = new HeapSort(); | ||
sort.heapSort(arr); | ||
sort.display(arr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package Sorting; | ||
|
||
/* | ||
Quicksort is a sorting algorithm, which is leveraging the divide-and-conquer principle. | ||
It has an average O(n log n) complexity and it’s one of the most used sorting algorithms, | ||
especially for big data volumes. | ||
*/ | ||
|
||
public class QuickSort { | ||
/* | ||
function that consider last element as pivot, place the pivot at its exact | ||
position, and place smaller elements to left of pivot and greater elements | ||
to right of pivot. | ||
*/ | ||
int partition (int a[], int start, int end) | ||
{ | ||
int pivot = a[end]; // pivot element | ||
int i = (start - 1); | ||
|
||
for (int j = start; j <= end - 1; j++) | ||
{ | ||
// If current element is smaller than the pivot | ||
if (a[j] < pivot) | ||
{ | ||
i++; // increment index of smaller element | ||
int t = a[i]; | ||
a[i] = a[j]; | ||
a[j] = t; | ||
} | ||
} | ||
int t = a[i+1]; | ||
a[i+1] = a[end]; | ||
a[end] = t; | ||
return (i + 1); | ||
} | ||
|
||
// function to implement quick sort | ||
void quick(int a[], int start, int end) // a[] = array to be sorted, start = Starting index, end = Ending index | ||
{ | ||
if (start < end) | ||
{ | ||
int p = partition(a, start, end); //p is partitioning index | ||
quick(a, start, p - 1); | ||
quick(a, p + 1, end); | ||
} | ||
} | ||
|
||
// function to print an array | ||
void printArray(int a[], int n) | ||
{ | ||
int i; | ||
for (i = 0; i < n; i++) | ||
System.out.print(a[i] + " "); | ||
} | ||
public static void main(String[] args) { | ||
int a[] = { 13, 18, 27, 2, 19, 25, 1, 23 }; | ||
int n = a.length; | ||
System.out.println("Before Sorting the Array: "); | ||
QuickSort sort = new QuickSort(); | ||
sort.printArray(a, n); | ||
sort.quick(a, 0, n - 1); | ||
System.out.println("\nAfter Quick Sorting the Array:"); | ||
sort.printArray(a, n); | ||
} | ||
} |