Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Siddhartha_sorting #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/pkg1/Siddhartha_sorting
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@



import java.util.Arrays;

public class Siddhartha_sorting {
public static void main(String args[]){
int arr[]={10,55,8,6,7,3};
arr= mergeSort(arr);
System.out.println(Arrays.toString(arr));
}
static int[] mergeSort(int [] arr){
if(arr.length == 1){
return arr;
}
int mid =arr.length/2;

int[] left = mergeSort(Arrays.copyOfRange(arr, 0, mid));
int[] right = mergeSort(Arrays.copyOfRange(arr, mid, arr.length));
return merge (left,right);
}
public static int[] merge(int [] first,int[] second){
int [] mix =new int [first.length+second.length];
int i=0;
int j=0;
int k=0;
while(i<first.length && j<second.length){
if (first[i]<second[j]){
mix[k]=first[i];
i++;
}else{
mix[k]=second[j];
j++;
}
k++;

} //to add the rest elements left either on i's side or j's side;
while(i<first.length){
mix[k]=first[i];
i++;
k++;}
while(j<second.length){
mix[k]=second[j];
j++;
k++;}

return mix;}
}