-
Notifications
You must be signed in to change notification settings - Fork 14
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
Fresh pull request for sorting algorithms #26
Conversation
@Ahel2000 do we need the .java files? You already added code in the markdown files. @arnabsen1729 opinion? |
|
||
/** | ||
* Write a description of class Insertion here. | ||
* | ||
* @author (your name) | ||
* @version (a version number or a date) | ||
*/ | ||
public class Insertion | ||
{ | ||
public static void sort(Comparable a[]) | ||
{ | ||
// initialise instance variables | ||
for(int i=0;i<a.length;i++){ | ||
for(int j=i;j>0;j--){ | ||
if(less(a[j],a[j-1])){ | ||
exch(a,j,j-1); | ||
} | ||
else{ | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static boolean less(Comparable v , Comparable w){ | ||
return v.compareTo(w)<0 ; | ||
} | ||
|
||
public static void exch(Comparable a[], int i, int j){ | ||
Comparable swap=a[i]; | ||
a[i]=a[j]; | ||
a[j]=swap; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this file.
|
||
Given an array ``arr = [a1,a2,a3,a4,a5...,an] `` , we need to sort the array the array with the help of Insertion Sort algorithm. | ||
|
||
--------------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use <hr>
|
||
|
||
|
||
----------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here as well remove these with <hr>
public class Merge{ | ||
public static Comparable[] theArray; | ||
public static Comparable[] sort(Comparable[] a){ | ||
theArray= new Comparable[a.length]; | ||
theArray=a; | ||
for(int i=0;i<a.length;i++){ | ||
System.out.print(theArray[i]+" "); | ||
} | ||
System.out.println(); | ||
Comparable[] workplace=new Comparable[a.length]; | ||
mergesort(workplace,0,a.length-1); | ||
for(int i=0;i<a.length;i++){ | ||
System.out.print(theArray[i]+" "); | ||
} | ||
System.out.println(); | ||
return theArray; | ||
} | ||
|
||
public static void mergesort(Comparable[] workplace,int lowerbound,int upperbound){ | ||
if(lowerbound==upperbound){ | ||
return; | ||
} | ||
else{ | ||
int mid=(lowerbound+upperbound)/2; | ||
mergesort(workplace,lowerbound,mid); | ||
mergesort(workplace,mid+1,upperbound); | ||
merge(workplace,lowerbound,mid+1,upperbound); | ||
} | ||
} | ||
|
||
public static void merge(Comparable[] workplace,int lowPtr,int highPtr,int upperbound) | ||
{ | ||
int low=lowPtr; | ||
int mid=highPtr-1; | ||
int j=0; | ||
int n=upperbound-low+1; | ||
while(low<=mid && highPtr<=upperbound){ | ||
if(theArray[low].compareTo(theArray[highPtr])<0){ | ||
workplace[j++]=theArray[low++]; | ||
}else{ | ||
workplace[j++]=theArray[highPtr++]; | ||
} | ||
} | ||
|
||
while(low<=mid){ | ||
workplace[j++]=theArray[low++]; | ||
} | ||
|
||
while(highPtr<=upperbound){ | ||
workplace[j++]=theArray[highPtr++]; | ||
} | ||
|
||
for(j=0;j<n;j++){ | ||
theArray[lowPtr+j]=workplace[j]; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File not needed
I have made the changes @arnabsen1729 |
|
||
## Code for the Algorithm | ||
<hr> | ||
### Code For Insertion Sort |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add some space here, markdown shenanigans
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check the comments, then I will merge. @arnabsen1729 have a look as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@allcontributors add @Ahel2000 for contributing to documentation |
I've put up a pull request to add @Ahel2000! 🎉 |
Description
Add insertion sort, selection sort and merge sort.
Issue #5
Type of change
Please delete options that are not relevant.
Added to SUMMARY.md (delete section if not needed)?
Have you added the page to SUMMARY.md if this is a new page and not a fix?