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

Fresh pull request for sorting algorithms #26

Merged
merged 19 commits into from
Nov 27, 2020
Merged

Conversation

Ahel2000
Copy link

Description

Add insertion sort, selection sort and merge sort.

Issue #5

Type of change

Please delete options that are not relevant.

  • Added Algorithm
  • Folder Structure

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?

  • Yes
  • No

@sadn1ck
Copy link

sadn1ck commented Nov 24, 2020

@Ahel2000 do we need the .java files? You already added code in the markdown files. @arnabsen1729 opinion?

@sadn1ck sadn1ck requested a review from arnabsen1729 November 24, 2020 16:32
Comment on lines 1 to 34

/**
* 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;
}
}
Copy link
Member

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.

---------------
Copy link
Member

@arnabsen1729 arnabsen1729 Nov 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use <hr>




-----------
Copy link
Member

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>

Comment on lines 1 to 57
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];
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File not needed

@Ahel2000
Copy link
Author

I have made the changes @arnabsen1729


## Code for the Algorithm
<hr>
### Code For Insertion Sort
Copy link

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

Copy link

@sadn1ck sadn1ck left a 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

Copy link

@sadn1ck sadn1ck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sadn1ck sadn1ck merged commit 7f90d50 into codeiiest-dev:main Nov 27, 2020
@sadn1ck
Copy link

sadn1ck commented Nov 27, 2020

@allcontributors add @Ahel2000 for contributing to documentation

@allcontributors
Copy link

@sadn1ck

I've put up a pull request to add @Ahel2000! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants