From e3b8e956c37549d7750449fd91ef6757c09fd041 Mon Sep 17 00:00:00 2001 From: Aman Chopra <56465233+Aman1905@users.noreply.github.com> Date: Mon, 4 Oct 2021 16:50:12 +0530 Subject: [PATCH 1/6] Add files via upload --- .../Java/Sorting Algos/mepty.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Program's_Contributed_By_Contributors/Java/Sorting Algos/mepty.txt diff --git a/Program's_Contributed_By_Contributors/Java/Sorting Algos/mepty.txt b/Program's_Contributed_By_Contributors/Java/Sorting Algos/mepty.txt new file mode 100644 index 0000000000..b9cd7c5263 --- /dev/null +++ b/Program's_Contributed_By_Contributors/Java/Sorting Algos/mepty.txt @@ -0,0 +1 @@ +nbvkbvbvnknvkjncx \ No newline at end of file From 6fc2cca36fdab322e6a848dd9f7f756a89559ed4 Mon Sep 17 00:00:00 2001 From: Aman Chopra <56465233+Aman1905@users.noreply.github.com> Date: Mon, 4 Oct 2021 16:51:42 +0530 Subject: [PATCH 2/6] Selection Sort --- .../Java/Sorting Algos/SelectionSort.java | 42 +++++++++++++++++++ .../Java/Sorting Algos/mepty.txt | 1 - 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 Program's_Contributed_By_Contributors/Java/Sorting Algos/SelectionSort.java delete mode 100644 Program's_Contributed_By_Contributors/Java/Sorting Algos/mepty.txt diff --git a/Program's_Contributed_By_Contributors/Java/Sorting Algos/SelectionSort.java b/Program's_Contributed_By_Contributors/Java/Sorting Algos/SelectionSort.java new file mode 100644 index 0000000000..e8f9e7d1ba --- /dev/null +++ b/Program's_Contributed_By_Contributors/Java/Sorting Algos/SelectionSort.java @@ -0,0 +1,42 @@ + +import java.util.*; + +public class SelectionSort { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int[] arr = { 4, 6, 7, 2, 1, 5, 3, 8, 9 }; + selectionSorting(arr); + System.out.println(Arrays.toString(arr)); + + } + + static void selectionSorting(int[] arr) { + for (int i = 0; i < arr.length; i++) { +// find the max item in the remaining array and swap with correct index + + int last = arr.length - i - 1; + + int maxInd = getMaxInd(arr, 0, last); + swapArray(arr, maxInd, last); + } + } + + private static int getMaxInd(int[] arr, int start, int end) { + int max = start; + + for (int i = start; i <= end; i++) { + if (arr[max] < arr[i]) + max = i; + } + return max; + } + + static void swapArray(int[] arr, int first, int second) { + int temp = arr[first]; + arr[first] = arr[second]; + arr[second] = temp; + } + +} diff --git a/Program's_Contributed_By_Contributors/Java/Sorting Algos/mepty.txt b/Program's_Contributed_By_Contributors/Java/Sorting Algos/mepty.txt deleted file mode 100644 index b9cd7c5263..0000000000 --- a/Program's_Contributed_By_Contributors/Java/Sorting Algos/mepty.txt +++ /dev/null @@ -1 +0,0 @@ -nbvkbvbvnknvkjncx \ No newline at end of file From c44c259472de2402c52068b5bb60b31d2bb2877c Mon Sep 17 00:00:00 2001 From: Aman Chopra <56465233+Aman1905@users.noreply.github.com> Date: Mon, 4 Oct 2021 16:52:19 +0530 Subject: [PATCH 3/6] Insertion Sort --- .../Java/Sorting Algos/InsertionSort.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Program's_Contributed_By_Contributors/Java/Sorting Algos/InsertionSort.java diff --git a/Program's_Contributed_By_Contributors/Java/Sorting Algos/InsertionSort.java b/Program's_Contributed_By_Contributors/Java/Sorting Algos/InsertionSort.java new file mode 100644 index 0000000000..e56d5678bf --- /dev/null +++ b/Program's_Contributed_By_Contributors/Java/Sorting Algos/InsertionSort.java @@ -0,0 +1,33 @@ + +import java.util.*; + +public class InsertionSort { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int[] arr = { 5, 3, 4, 1, 2 }; + insertionSorting(arr); + System.out.println(Arrays.toString(arr)); + + } + + static void insertionSorting(int[] arr) { + for (int i = 0; i < arr.length - 1; i++) { + for (int j = i + 1; j > 0; j--) { + if (arr[j] < arr[j - 1]) { + swapArray(arr, j, j - 1); + } else { + break; + } + } + } + } + + static void swapArray(int[] arr, int first, int second) { + int temp = arr[first]; + arr[first] = arr[second]; + arr[second] = temp; + } + +} From a2367341831fac6834da6b393b3f322fa904019b Mon Sep 17 00:00:00 2001 From: Aman Chopra <56465233+Aman1905@users.noreply.github.com> Date: Mon, 4 Oct 2021 16:53:03 +0530 Subject: [PATCH 4/6] Cyclic Sort --- .../Java/Sorting Algos/CyclicSort.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Program's_Contributed_By_Contributors/Java/Sorting Algos/CyclicSort.java diff --git a/Program's_Contributed_By_Contributors/Java/Sorting Algos/CyclicSort.java b/Program's_Contributed_By_Contributors/Java/Sorting Algos/CyclicSort.java new file mode 100644 index 0000000000..ef18d82eee --- /dev/null +++ b/Program's_Contributed_By_Contributors/Java/Sorting Algos/CyclicSort.java @@ -0,0 +1,43 @@ + +import java.util.*; + +public class CyclicSort { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int[] arr = {3, 5, 2, 1, 4}; +// always remember it should be continous + + sorting(arr); + System.out.println(Arrays.toString(arr)); + + } + +// Algo :- +// 1. Start looping from i +// 2. Put it at corrct index +// 3. Only move i when the element at i is sorted + + static void sorting(int[] arr) { + int i = 0; + while (i < arr.length) { + int correctInd = arr[i] - 1; +// this should be the correct index + + if (arr[i] != arr[correctInd]) { + swap(arr, i, correctInd); + } else { + i++; + } + } + } + + static void swap(int[] arr, int first, int second) { + int temp = arr[first]; + arr[first] = arr[second]; + arr[second] = temp; + + } + +} From fa0f2def42cb5d6c416aeb3179cb053f0e7189dc Mon Sep 17 00:00:00 2001 From: Aman Chopra <56465233+Aman1905@users.noreply.github.com> Date: Mon, 4 Oct 2021 16:53:37 +0530 Subject: [PATCH 5/6] Bubble Sort --- .../Java/Sorting Algos/BubbleSort.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Program's_Contributed_By_Contributors/Java/Sorting Algos/BubbleSort.java diff --git a/Program's_Contributed_By_Contributors/Java/Sorting Algos/BubbleSort.java b/Program's_Contributed_By_Contributors/Java/Sorting Algos/BubbleSort.java new file mode 100644 index 0000000000..d37750187c --- /dev/null +++ b/Program's_Contributed_By_Contributors/Java/Sorting Algos/BubbleSort.java @@ -0,0 +1,41 @@ + + +import java.util.Arrays; + +public class BubbleSort { + + public static void main(String[] args) { + int[] arr = { 5, 4, 3, 2, 1 }; + bubble(arr); + System.out.println(Arrays.toString(arr)); + + } + + static void bubble(int[] arr) { + + boolean swapped; +// run the steps N-1 times + + for (int i = 0; i < arr.length; i++) { + swapped = false; +// for each step, max item will come at last respective index + + for (int j = 1; j < arr.length - i; j++) { +// swap if the items are smaller than previous item + + if (arr[j] < arr[j - 1]) { + int temp = arr[j]; + arr[j] = arr[j - 1]; + arr[j - 1] = temp; + swapped = true; + } + } + +// if you did not swap for a particular value of i, it means array is sorted hence stop program + + if(!swapped) + break; + } + } + +} From b457d9012db3b1a6e9a61ce7507cab14ff57820a Mon Sep 17 00:00:00 2001 From: Aman Chopra <56465233+Aman1905@users.noreply.github.com> Date: Mon, 4 Oct 2021 16:54:34 +0530 Subject: [PATCH 6/6] added Aman Chopra in Contributions page --- Contributors.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Contributors.html b/Contributors.html index ddffa5f477..25d6f767d8 100644 --- a/Contributors.html +++ b/Contributors.html @@ -597,7 +597,7 @@ Aditya Krishna Yash jain Tharaka Bandara - +Aman Chopra