Anda di halaman 1dari 4

/**

* Author: Aracely Macias


* Due Date: 28FEB2011
*
* Course: COSC2010
* Assignment: Assignment 3, SortArray.java
* Purpose: Fill an array with 1000 random integers. Use the fuctions
* mergeSort and quick sort to sort the array seperately. Compute
* the time it takes to sort using each algorithm and print it.
*
*/

import java.util.*;
public class SortArray
{
public static int maxNum = 1000;
//public static int maxNum = 100000;
//public static int maxNum = 10;
public static int[] randArray = new int[maxNum];
public static Random randGen = new Random(1239480);
public static long startTime;
public static long startTime2;
public static long endTime;
public static long endTime2;
public static long elapsedTime;
public static long elapsedTime2;

public static void main(String [] args)


{
//System.out.println("This is the unsorted array: ");
fillArray();
System.out.println("");
//printSortedArray();

mergeSort(randArray, 0, randArray.length);
System.out.println("The merge sort algorithm took: " + elapsedTime
+ " ms");

quickSort(randArray, 0, randArray.length-1);
System.out.println("The quick sort algorithm took: " + elapsedTime2
+ " ms");
System.out.println("");

}// end main()

public static void fillArray()


{
System.out.println("Filling array with "+ maxNum+" ints...");
for(int i = 0; i< randArray.length; i++)
{
randArray[i] = randGen.nextInt(maxNum);
//System.out.print(randArray[i] + " " );
}
System.out.println("Done filling array");
}// end fillArray()
/*
public static void printSortedArray()
{
System.out.println("This is the sorted array: ");
for (int i = 0; i < randArray.length; i++)
{
System.out.print(randArray[i] + " ");
}
System.out.println();
} // end printSortedArrays()
*/

public static void mergeSort(int[] rList, int first, int n)


{
/**
* The first time mergeSort is called, the method is sorting
* data[first] through data[firstHalf]
* The second time mergeSort is called, the method is sorting
* data[firstHalf] to the end of the second half
*
* 'rList' is the array to be sorted
* 'first' is the starting index of array to be sorted
* 'n' is the number of elements to be sorted
*/

startTime = System.currentTimeMillis();
int firstHalf; // Size of the first half of the array
int secondHalf; // Size of the second half of the array

if (n > 1)
{
// Compute sizes of the two halves
firstHalf = n / 2;
secondHalf = n - firstHalf;

mergeSort(rList, first, firstHalf);


mergeSort(rList, first + firstHalf, secondHalf);
merge(rList, first, firstHalf, secondHalf);
}
} // end mergeSort()

public static void merge(int[] rList, int first, int array1, int array2)
{
/**
* --------This method merges the two sorted halves---------
*
* numTempElements = number of elements copied from rList to temp
* numFirstElements = number copied from the first half of data
* numSecndElements = number copied from the second half of data

*/

int[] temp = new int[array1+array2]; // Allocate the temporary array


int numTempElements = 0;
int numFirstElements = 0;
int numSecndElements = 0;
//Merge elements, copying from two halves of data to the temporary array.
while ((numFirstElements < array1) && (numSecndElements < array2))
{
if (rList[first + numFirstElements] <
rList[first + array1 + numSecndElements])
{
temp[numTempElements++] = rList[first + (numFirstElements++)];
}
else
temp[numTempElements++] = rList[first + array1 +
(numSecndElements++)];
}

// Copy any remaining entries in the left and right subarrays.


while (numFirstElements < array1) {
temp[numTempElements++] = rList[first + (numFirstElements++)];
}
while (numSecndElements < array2) {
temp[numTempElements++] = rList[first + array1 +
(numSecndElements++)];
}

// Copy from temp back to the data array.


for (int i = 0; i < array1+array2; i++) {
rList[first + i] = temp[i];
}
endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
} // end merge()

public static void quickSort(int array[],int low, int hi)


{
startTime2 = System.currentTimeMillis();
int i=low;
int j=hi;
int h;

int x=randArray[(low+hi)/2]; // comparison element x

// partition
do
{
while (randArray[i]<x) i++;
while (randArray[j]>x) j--;
if (i<=j)
{
h=randArray[i]; randArray[i]=randArray[j]; randArray[j]=h;
i++; j--;
}

} while (i<=j);

// recursion
if (low<j) quickSort(randArray, low, j);
if (i<hi) quickSort(randArray, i, hi);
endTime2 = System.currentTimeMillis();
elapsedTime2 = endTime2- startTime2;
} // end quickSort()

Anda mungkin juga menyukai