0% found this document useful (0 votes)
59 views

CS 1103 Programming Assignment Unit 1

This programming assignment involves benchmarking different sorting algorithms by timing how long they take to sort arrays of random numbers. The code provided generates two arrays of random integers, sorts one using selection sort and the other using Java's built-in Arrays.sort, and prints the run times of each to compare their performance. Selection sort is implemented to sort the arrays in increasing order by iteratively finding the maximum value and swapping it into the end of the sorted portion of the array.

Uploaded by

Patrick Maina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

CS 1103 Programming Assignment Unit 1

This programming assignment involves benchmarking different sorting algorithms by timing how long they take to sort arrays of random numbers. The code provided generates two arrays of random integers, sorts one using selection sort and the other using Java's built-in Arrays.sort, and prints the run times of each to compare their performance. Selection sort is implemented to sort the arrays in increasing order by iteratively finding the maximum value and swapping it into the end of the sorted portion of the array.

Uploaded by

Patrick Maina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 java.util.Arrays;
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 < sortingArray1.length; i++) {
//The filling it have two arrays with the same random numbers.
sortingArray1[i] = (int)(Integer.MAX_VALUE * Math.random());
sortingArray2[i] = sortingArray1[i];
}
long startTimeArray1 = System.currentTimeMillis();
//It starts computing for the time of selection it arranges.
selectionSort(sortingArray1);
//The arranging Array1 with selectionSort
long runTimeArray1 = System.currentTimeMillis() - startTimeArray1;
//The time to run the selectionSort.
long startTimeArray2 = System.currentTimeMillis();
//It starts computing in the time for Array.sort
Arrays.sort(sortingArray2);
//The arranging Array2 with Arrays.sort
long runTimeArray2 = System.currentTimeMillis() - startTimeArray2;
//It times to run Array.sort
System.out.println("Selection sort time(sec):"+runTimeArray1/1000.0);
System.out.println("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 = A.length-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

You might also like