0% found this document useful (0 votes)
43 views2 pages

Public Int Public Int Public Int: String

The document defines a method to sort an array of strings using a merge sort algorithm. It takes in an input array, copies it to a temporary array, recursively divides the array in half and sorts each half, and then merges the sorted halves back into the original array.

Uploaded by

Vikki Pang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Public Int Public Int Public Int: String

The document defines a method to sort an array of strings using a merge sort algorithm. It takes in an input array, copies it to a temporary array, recursively divides the array in half and sorts each half, and then merges the sorted halves back into the original array.

Uploaded by

Vikki Pang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

public int[] array;

public int[] tempArr;


public int length;

public void sort(String inputArr[])


{
array = inputArr;
length = inputArr.length;
tempArr = new String[length];
doSort(0, length - 1);
}

private void doSort(int lowerIndex, int higherIndex)


{
if (lowerIndex < higherIndex)
{
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
doSort(lowerIndex, middle);
doSort(middle + 1, higherIndex);
doSomething(lowerIndex, middle, higherIndex);
}
}

private void doSomething(int lowerIndex, int middle, int higherIndex)


{
for (int i = lowerIndex; i <= higherIndex; i++)
{
tempArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;

while (i <= middle && j <= higherIndex)


{
if (tempArr[i] <= tempArr[j])
{
array[k] = tempArr[i];
i++;
}
else {
array[k] = tempArr[j];
j++;
}
k++;
}
while (i <= middle)
{
array[k] = tempArr[i];
k++;
i++;
}
}

You might also like