From f6f864c89d6eeb3dafd452f0871fbaad057808af Mon Sep 17 00:00:00 2001 From: Mayur Vaid <34806097+MayV@users.noreply.github.com> Date: Tue, 2 Oct 2018 18:02:15 +0530 Subject: [PATCH 1/5] Added new sorting algorithms. --- Sorting algorithms/BubbleSort.java | 39 +++++++++++++++++ Sorting algorithms/MergeSort.java | 63 +++++++++++++++++++++++++++ Sorting algorithms/QuickSort.java | 49 +++++++++++++++++++++ Sorting algorithms/SelectionSort.java | 39 +++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 Sorting algorithms/BubbleSort.java create mode 100644 Sorting algorithms/MergeSort.java create mode 100644 Sorting algorithms/QuickSort.java create mode 100644 Sorting algorithms/SelectionSort.java diff --git a/Sorting algorithms/BubbleSort.java b/Sorting algorithms/BubbleSort.java new file mode 100644 index 0000000..05d3665 --- /dev/null +++ b/Sorting algorithms/BubbleSort.java @@ -0,0 +1,39 @@ +package Sorting; + +import java.util.Scanner; + +public class Bubble_sort { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Scanner s = new Scanner(System.in); + System.out.println("Enter size of array: "); + int n=s.nextInt(); + int[] arr=new int[5]; + System.out.println("Enter elements of array: "); + for(int i=0;iarr[j+1]) + { + temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } + } + } + +} \ No newline at end of file diff --git a/Sorting algorithms/MergeSort.java b/Sorting algorithms/MergeSort.java new file mode 100644 index 0000000..cef2916 --- /dev/null +++ b/Sorting algorithms/MergeSort.java @@ -0,0 +1,63 @@ +package Sorting; + +public class MergeSort { + + public static void main(String[] args) { + // TODO Auto-generated method stub + int[] arr = {8,7,10,6,9,3}; + int[] ans=MergeSort(arr,0,arr.length-1); + for(int i=0;istart) //Left SubArray contains more than 1 element. + quicksort(arr,start,indx-1); + if(indx+1=arr[pivot] && right!=pivot) + right--; + if(right==pivot) + return pivot; + temp = arr[pivot]; + arr[pivot] = arr[right]; + arr[right] = temp; + pivot = right; + while(arr[left]<=arr[pivot] && left!=pivot) + left++; + if(left==pivot) + return pivot; + temp = arr[pivot]; + arr[pivot] = arr[left]; + arr[left] = temp; + pivot = left; + } + } + +} diff --git a/Sorting algorithms/SelectionSort.java b/Sorting algorithms/SelectionSort.java new file mode 100644 index 0000000..cdb0539 --- /dev/null +++ b/Sorting algorithms/SelectionSort.java @@ -0,0 +1,39 @@ +package Sorting; + +import java.util.Scanner; + +public class SelectionSort { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Scanner s = new Scanner(System.in); + System.out.println("Enter Size of array: "); + int n=s.nextInt(); + int[] arr=new int[n]; + System.out.println("Enter elements of array: "); + for(int i=0;iarr[j]) + min=j; + } + temp=arr[i]; + arr[i]=arr[min]; + arr[min]=temp; + } + } + +} From 8a90240255c434730a0184c916bad3a11742be1c Mon Sep 17 00:00:00 2001 From: Mayur Vaid <34806097+MayV@users.noreply.github.com> Date: Tue, 2 Oct 2018 18:12:01 +0530 Subject: [PATCH 2/5] Update BubbleSort.java --- Sorting algorithms/BubbleSort.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sorting algorithms/BubbleSort.java b/Sorting algorithms/BubbleSort.java index 05d3665..c949188 100644 --- a/Sorting algorithms/BubbleSort.java +++ b/Sorting algorithms/BubbleSort.java @@ -1,5 +1,3 @@ -package Sorting; - import java.util.Scanner; public class Bubble_sort { @@ -36,4 +34,4 @@ public static void Bubble_Sort(int[] arr) } } -} \ No newline at end of file +} From 9910d4fb14dfa101870fc3ba7aec75cce67a234a Mon Sep 17 00:00:00 2001 From: Mayur Vaid <34806097+MayV@users.noreply.github.com> Date: Tue, 2 Oct 2018 18:12:26 +0530 Subject: [PATCH 3/5] Update MergeSort.java --- Sorting algorithms/MergeSort.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sorting algorithms/MergeSort.java b/Sorting algorithms/MergeSort.java index cef2916..e4cc5d9 100644 --- a/Sorting algorithms/MergeSort.java +++ b/Sorting algorithms/MergeSort.java @@ -1,5 +1,3 @@ -package Sorting; - public class MergeSort { public static void main(String[] args) { @@ -60,4 +58,4 @@ public static int[] Merge(int[] a1,int[] a2) return ans; } -} \ No newline at end of file +} From e776109b3cde6c621259888f7bf04bedfc4a9d6a Mon Sep 17 00:00:00 2001 From: Mayur Vaid <34806097+MayV@users.noreply.github.com> Date: Tue, 2 Oct 2018 18:12:48 +0530 Subject: [PATCH 4/5] Update QuickSort.java --- Sorting algorithms/QuickSort.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/Sorting algorithms/QuickSort.java b/Sorting algorithms/QuickSort.java index 07c5385..3e5d409 100644 --- a/Sorting algorithms/QuickSort.java +++ b/Sorting algorithms/QuickSort.java @@ -1,5 +1,3 @@ -package Sorting; - public class QuickSort { public static void main(String[] args) { From 00ae783b020c3a6112bdcf3d4e7d6cd76234623f Mon Sep 17 00:00:00 2001 From: Mayur Vaid <34806097+MayV@users.noreply.github.com> Date: Tue, 2 Oct 2018 18:13:08 +0530 Subject: [PATCH 5/5] Update SelectionSort.java --- Sorting algorithms/SelectionSort.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/Sorting algorithms/SelectionSort.java b/Sorting algorithms/SelectionSort.java index cdb0539..6ac42c3 100644 --- a/Sorting algorithms/SelectionSort.java +++ b/Sorting algorithms/SelectionSort.java @@ -1,5 +1,3 @@ -package Sorting; - import java.util.Scanner; public class SelectionSort {