CS 1103 programming assignment unit 1
This week's assignment is Part 1: Benchmarking Sorting Algorithms from Lab 2 of this unit
Codes examples
import [Link];
public class BenchmarkingSortingAlgorithms {
public static void main(String[] args) {
int maxArraySize = 10000; //The array size.
int[] sortingArray1 = new int [maxArraySize]; //The first array.
int[] sortingArray2 = new int [maxArraySize]; //The second array
//class constructor
for (int i = 0; i < [Link]; i++) {
//The filling it have two arrays with the same random numbers.
sortingArray1[i] = (int)(Integer.MAX_VALUE * [Link]());
sortingArray2[i] = sortingArray1[i];
}
long startTimeArray1 = [Link]();
//It starts computing for the time of selection it arranges.
selectionSort(sortingArray1);
//The arranging Array1 with selectionSort
long runTimeArray1 = [Link]() - startTimeArray1;
//The time to run the selectionSort.
long startTimeArray2 = [Link]();
//It starts computing in the time for [Link]
[Link](sortingArray2);
//The arranging Array2 with [Link]
long runTimeArray2 = [Link]() - startTimeArray2;
//It times to run [Link]
[Link]("Selection sort time(sec):"+runTimeArray1/1000.0);
[Link]("Selection sort time(sec):"+runTimeArray2/1000.0);
}
static void selectionSort(int[] A) {
//The sort of A in increasing in order, using selection sort.
for(int lastPlace = [Link]-1; lastPlace > 0; lastPlace--){
//It finds the biggest item among on it A[0], A[1], ...,
lOMoARcPSD|9696129
//The A[last place], and move it into the position last place in
exchange it with the number
//that is currently position in the last place.
int maxLoc = 0;
for (int j = 1; j <= lastPlace; j++)
{
if(A[j] > A[maxLoc]) {
//The since A[j] is larger than the maximum., J is the new
location of the maximum value.
maxLoc = j;
}
}
//The exchange is the biggest item with A[lastPlace].
int temp = A[maxLoc];
A[maxLoc] = A[lastPlace];
A[lastPlace] = temp;
}
}
}
Out put
Selection sort time(sec):0.119
Selection sort time(sec):0.023