-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from Ashirz/master
Added sorting algorithms files
- Loading branch information
Showing
2 changed files
with
240 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,135 @@ | ||
/*Algorithms for Quick sort and Merge sort using recursive approach*/ | ||
|
||
public class SortingAlgorithms1 { | ||
|
||
public static void main(String[] args) { | ||
int array[] = { 5, 8, 9, 1, 2, 6, 4 }; | ||
|
||
sort(array, 0, array.length - 1); | ||
System.out.println("After Quick Sort:"); | ||
printArray(array); | ||
|
||
// Reset the array for subsequent sorts | ||
array = new int[] { 5, 8, 9, 1, 2, 6, 4 }; | ||
|
||
divide(array); | ||
System.out.println("After Merge Sort:"); | ||
printArray(array); | ||
|
||
} | ||
|
||
// Helper method to print the array | ||
public static void printArray(int[] arr) { | ||
for (int i : arr) { | ||
System.out.print(i + " "); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
// Merge sort methods divide & conquer | ||
public static void divide(int[] array) { | ||
|
||
int length = array.length; | ||
|
||
int mid = length / 2; | ||
|
||
if (length <= 1) | ||
return; | ||
|
||
// Subarrays | ||
int[] LeftArray = new int[mid]; | ||
int[] RightArray = new int[length - mid]; | ||
|
||
int i = 0, j = 0; | ||
|
||
// populating subarrays | ||
for (; i < length; i++) { | ||
if (i < mid) { | ||
LeftArray[i] = array[i]; | ||
|
||
} else { | ||
RightArray[j] = array[i]; | ||
j++; | ||
} | ||
|
||
} | ||
|
||
divide(LeftArray); | ||
|
||
divide(RightArray); | ||
|
||
conquer(LeftArray, RightArray, array); | ||
|
||
} | ||
|
||
private static void conquer(int[] leftArray, int[] rightArray, int[] array) { | ||
int length = array.length; | ||
int leftsize = length / 2; | ||
int rightsize = length - leftsize; | ||
|
||
int l = 0, r = 0, i = 0; | ||
|
||
// populating the sorted array back | ||
while (l < leftsize && r < rightsize) { | ||
if (leftArray[l] < rightArray[r]) { | ||
array[i] = leftArray[l]; | ||
i++; | ||
l++; | ||
} else { | ||
array[i] = rightArray[r]; | ||
i++; | ||
r++; | ||
} | ||
|
||
} | ||
|
||
while (l < leftsize) { | ||
array[i] = leftArray[l]; | ||
i++; | ||
l++; | ||
} | ||
|
||
while (r < rightsize) { | ||
array[i] = rightArray[r]; | ||
i++; | ||
r++; | ||
} | ||
|
||
} | ||
|
||
// Quick Sort methods sort and partition | ||
private static void sort(int[] array, int start, int end) { | ||
if (end <= start) | ||
return; | ||
|
||
int pivot = partition(array, start, end); | ||
sort(array, start, pivot - 1); | ||
sort(array, pivot + 1, end); | ||
|
||
} | ||
|
||
private static int partition(int[] array, int start, int end) { | ||
|
||
int pivot = array[end]; | ||
int j = start-1; | ||
|
||
for (int i=start; i<array.length; i++) { | ||
if(array[i]<pivot) { | ||
j++; | ||
int temp; | ||
|
||
temp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = temp; | ||
} | ||
} | ||
|
||
j++; | ||
|
||
int temp = array[j]; | ||
array[j] = array[end]; | ||
array[end] =temp; | ||
|
||
return j; | ||
} | ||
} |
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,105 @@ | ||
/*Bubble sort, Selection sort and Insertion sort Algorithms using iterative approach*/ | ||
|
||
public class SortingAlgorithms2 { | ||
|
||
public static void main(String[] args) { | ||
int arr[] = { 5, 8, 9, 1, 2, 6, 4 }; | ||
|
||
System.out.println("Original Array:"); | ||
printArray(arr); | ||
|
||
bubbleSort(arr); | ||
System.out.println("After Bubble Sort:"); | ||
printArray(arr); | ||
|
||
// Reset the array for subsequent sorts | ||
arr = new int[] { 5, 8, 9, 1, 2, 6, 4 }; | ||
|
||
selectionSort(arr); | ||
System.out.println("After Selection Sort:"); | ||
printArray(arr); | ||
|
||
// Reset the array for subsequent sorts | ||
arr = new int[] { 5, 8, 9, 1, 2, 6, 4 }; | ||
|
||
insertionSort(arr); | ||
System.out.println("After Insertion Sort:"); | ||
printArray(arr); | ||
} | ||
|
||
/** | ||
* Sorts an array using the Bubble Sort algorithm. | ||
* Time Complexity: O(n^2) in the worst and average case. | ||
* Space Complexity: O(1) | ||
* | ||
* @param arr the array to be sorted | ||
*/ | ||
public static void bubbleSort(int[] arr) { | ||
int n = arr.length; | ||
for (int i = 0; i < n - 1; i++) { | ||
for (int j = 0; j < n - i - 1; j++) { | ||
if (arr[j] > arr[j + 1]) { | ||
// Swap arr[j] and arr[j+1] | ||
int temp = arr[j]; | ||
arr[j] = arr[j + 1]; | ||
arr[j + 1] = temp; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Sorts an array using the Selection Sort algorithm. | ||
* Time Complexity: O(n^2) in all cases. | ||
* Space Complexity: O(1) | ||
* | ||
* @param arr the array to be sorted | ||
*/ | ||
public static void selectionSort(int[] arr) { | ||
int n = arr.length; | ||
for (int i = 0; i < n - 1; i++) { | ||
int minIndex = i; | ||
for (int j = i + 1; j < n; j++) { | ||
if (arr[j] < arr[minIndex]) { | ||
minIndex = j; | ||
} | ||
} | ||
// Swap arr[i] and arr[minIndex] | ||
int temp = arr[i]; | ||
arr[i] = arr[minIndex]; | ||
arr[minIndex] = temp; | ||
} | ||
} | ||
|
||
/** | ||
* Sorts an array using the Insertion Sort algorithm. | ||
* Time Complexity: O(n^2) in the worst case, O(n) in the best case. | ||
* Space Complexity: O(1) | ||
* | ||
* @param arr the array to be sorted | ||
*/ | ||
public static void insertionSort(int[] arr) { | ||
int n = arr.length; | ||
for (int i = 1; i < n; ++i) { | ||
int key = arr[i]; | ||
int j = i - 1; | ||
|
||
/* Move elements of arr[0..i-1], that are | ||
greater than key, to one position ahead | ||
of their current position */ | ||
while (j >= 0 && arr[j] > key) { | ||
arr[j + 1] = arr[j]; | ||
j = j - 1; | ||
} | ||
arr[j + 1] = key; | ||
} | ||
} | ||
|
||
// Helper method to print the array | ||
public static void printArray(int[] arr) { | ||
for (int i : arr) { | ||
System.out.print(i + " "); | ||
} | ||
System.out.println(); | ||
} | ||
} |