Skip to content

Commit 3ec4913

Browse files
committed
#Modification 53
1 parent 7cf0b99 commit 3ec4913

4 files changed

Lines changed: 269 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package sortingAlgorithms;
2+
3+
public class InsertionSort {
4+
5+
/*Function to sort array using insertion sort*/
6+
void sort(int arr[])
7+
{
8+
int n = arr.length;
9+
for (int i = 1; i < n; ++i) {
10+
int key = arr[i];
11+
int j = i - 1;
12+
13+
while (j >= 0 && arr[j] > key) {
14+
arr[j + 1] = arr[j];
15+
j = j - 1;
16+
}
17+
arr[j + 1] = key;
18+
}
19+
}
20+
21+
/* A utility function to print array of size n*/
22+
static void printArray(int arr[])
23+
{
24+
int n = arr.length;
25+
for (int i = 0; i < n; ++i)
26+
System.out.print(arr[i] + " ");
27+
28+
System.out.println();
29+
}
30+
31+
// Driver method
32+
public static void main(String args[])
33+
{
34+
int arr[] = { 12, 11, 13, 5, 6 };
35+
36+
InsertionSort ob = new InsertionSort();
37+
ob.sort(arr);
38+
39+
printArray(arr);
40+
}
41+
}

sortingAlgorithms/MergeSort.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package sortingAlgorithms;
2+
3+
public class MergeSort {
4+
// Merges two subarrays of arr[].
5+
// First subarray is arr[l..m]
6+
// Second subarray is arr[m+1..r]
7+
void merge(int arr[], int l, int m, int r) {
8+
// Find sizes of two subarrays to be merged
9+
int n1 = m - l + 1;
10+
int n2 = r - m;
11+
12+
/* Create temp arrays */
13+
int L[] = new int[n1];
14+
int R[] = new int[n2];
15+
16+
/*Copy data to temp arrays*/
17+
for (int i = 0; i < n1; ++i)
18+
L[i] = arr[l + i];
19+
for (int j = 0; j < n2; ++j)
20+
R[j] = arr[m + 1 + j];
21+
22+
/* Merge the temp arrays */
23+
24+
// Initial indexes of first and second subarrays
25+
int i = 0, j = 0;
26+
27+
// Initial index of merged subarry array
28+
int k = l;
29+
while (i < n1 && j < n2) {
30+
if (L[i] <= R[j]) {
31+
arr[k] = L[i];
32+
i++;
33+
}
34+
else {
35+
arr[k] = R[j];
36+
j++;
37+
}
38+
k++;
39+
}
40+
41+
/* Copy remaining elements of L[] if any */
42+
while (i < n1) {
43+
arr[k] = L[i];
44+
i++;
45+
k++;
46+
}
47+
48+
/* Copy remaining elements of R[] if any */
49+
while (j < n2) {
50+
arr[k] = R[j];
51+
j++;
52+
k++;
53+
}
54+
}
55+
56+
// Main function that sorts arr[l..r] using
57+
// merge()
58+
void sort(int arr[], int l, int r)
59+
{
60+
if (l < r) {
61+
// Find the middle point
62+
int m =l+ (r-l)/2;
63+
64+
// Sort first and second halves
65+
sort(arr, l, m);
66+
sort(arr, m + 1, r);
67+
68+
// Merge the sorted halves
69+
merge(arr, l, m, r);
70+
}
71+
}
72+
73+
/* A utility function to print array of size n */
74+
static void printArray(int arr[])
75+
{
76+
int n = arr.length;
77+
for (int i = 0; i < n; ++i)
78+
System.out.print(arr[i] + " ");
79+
System.out.println();
80+
}
81+
82+
// Driver code
83+
public static void main(String args[])
84+
{
85+
int arr[] = { 12, 11, 13, 5, 6, 7 };
86+
87+
System.out.println("Given Array");
88+
printArray(arr);
89+
90+
MergeSort ob = new MergeSort();
91+
ob.sort(arr, 0, arr.length - 1);
92+
93+
System.out.println("\nSorted array");
94+
printArray(arr);
95+
}
96+
}

sortingAlgorithms/QuickSort.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package sortingAlgorithms;
2+
3+
import java.io.*;
4+
5+
public class QuickSort{
6+
7+
// A utility function to swap two elements
8+
static void swap(int[] arr, int i, int j)
9+
{
10+
int temp = arr[i];
11+
arr[i] = arr[j];
12+
arr[j] = temp;
13+
}
14+
15+
/* This function takes last element as pivot, places
16+
the pivot element at its correct position in sorted
17+
array, and places all smaller (smaller than pivot)
18+
to left of pivot and all greater elements to right
19+
of pivot */
20+
static int partition(int[] arr, int low, int high)
21+
{
22+
23+
// pivot
24+
int pivot = arr[high];
25+
26+
// Index of smaller element and
27+
// indicates the right position
28+
// of pivot found so far
29+
int i = (low - 1);
30+
31+
for(int j = low; j <= high - 1; j++)
32+
{
33+
34+
// If current element is smaller
35+
// than the pivot
36+
if (arr[j] < pivot)
37+
{
38+
39+
// Increment index of
40+
// smaller element
41+
i++;
42+
swap(arr, i, j);
43+
}
44+
}
45+
swap(arr, i + 1, high);
46+
return (i + 1);
47+
}
48+
49+
/* The main function that implements QuickSort
50+
arr[] --> Array to be sorted,
51+
low --> Starting index,
52+
high --> Ending index
53+
*/
54+
static void quickSort(int[] arr, int low, int high)
55+
{
56+
if (low < high)
57+
{
58+
59+
// pi is partitioning index, arr[p]
60+
// is now at right place
61+
int pi = partition(arr, low, high);
62+
63+
// Separately sort elements before
64+
// partition and after partition
65+
quickSort(arr, low, pi - 1);
66+
quickSort(arr, pi + 1, high);
67+
}
68+
}
69+
70+
// Function to print an array
71+
static void printArray(int[] arr, int size)
72+
{
73+
for(int i = 0; i < size; i++)
74+
System.out.print(arr[i] + " ");
75+
76+
System.out.println();
77+
}
78+
79+
// Driver Code
80+
public static void main(String[] args)
81+
{
82+
int[] arr = { 10, 7, 8, 9, 1, 5 };
83+
int n = arr.length;
84+
85+
quickSort(arr, 0, n - 1);
86+
System.out.println("Sorted array: ");
87+
printArray(arr, n);
88+
}
89+
}
90+
91+
// This code is contributed by Aman Soni
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package sortingAlgorithms;
2+
3+
public class SelectionSort {
4+
5+
void sort(int arr[])
6+
{
7+
int n = arr.length;
8+
9+
// One by one move boundary of unsorted sub-array
10+
for (int i = 0; i < n-1; i++) {
11+
// Find the minimum element in unsorted array
12+
int min_idx = i;
13+
for (int j = i+1; j < n; j++)
14+
if (arr[j] < arr[min_idx])
15+
min_idx = j;
16+
17+
// Swap the found minimum element with the first
18+
// element
19+
int temp = arr[min_idx];
20+
arr[min_idx] = arr[i];
21+
arr[i] = temp;
22+
}
23+
}
24+
25+
// Prints the array
26+
void printArray(int arr[]) {
27+
int n = arr.length;
28+
for (int i=0; i<n; ++i)
29+
System.out.print(arr[i]+" ");
30+
System.out.println();
31+
}
32+
33+
// Driver code to test above
34+
public void main(String args[]){
35+
SelectionSort ob = new SelectionSort();
36+
int arr[] = {5,11,13,6,12};
37+
ob.sort(arr);
38+
System.out.println("Sorted array");
39+
ob.printArray(arr);
40+
}
41+
}

0 commit comments

Comments
 (0)