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

changes done to sort #15

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/main/java/sortalgorithms/Sort.java
@@ -1,11 +1,61 @@
package sortalgorithms;

import java.util.Arrays;

public class Sort {


private int[] inputArray;


//////////////////////////////////////
public static int[] mergeSort(int [ ] inputArray)
{

int[] inputArray2 = inputArray.clone();

int[] temporaryArray = new int[inputArray2.length];
mergeSort(inputArray2, temporaryArray, 0, inputArray2.length - 1);
return inputArray2;
}


private static void mergeSort(int [ ] inputAray, int [ ] temporaryArray, int leftIndex, int rightIndex)
{
if( leftIndex < rightIndex )
{
int middle = (leftIndex + rightIndex) / 2;
mergeSort(inputAray, temporaryArray, leftIndex, middle);
mergeSort(inputAray, temporaryArray, middle + 1, rightIndex);
merge(inputAray, temporaryArray, leftIndex, middle + 1, rightIndex);
}
}


private static void merge(int[ ] inputArray, int[ ] temporaryArray, int leftIndex, int rightIndex, int endIndex )
{
int leftEnd = rightIndex - 1;
int k = leftIndex;
int num = endIndex - leftIndex + 1;

while(leftIndex <= leftEnd && rightIndex <= endIndex)
if(inputArray[leftIndex] < inputArray[rightIndex])
temporaryArray[k++] = inputArray[leftIndex++];
else
temporaryArray[k++] = inputArray[rightIndex++];

while(leftIndex <= leftEnd)
temporaryArray[k++] = inputArray[leftIndex++];

while(rightIndex <= endIndex)
temporaryArray[k++] = inputArray[rightIndex++];

for(int i = 0; i < num; i++, endIndex--)
inputArray[endIndex] = temporaryArray[endIndex];
}
///////////////////////////////////////



public int[] bubbleSort(int[] inputArray){

int size=inputArray.length-1;
Expand Down