Hello, Im a computer science major and i'm in an Analysis of Algorithms class. My professor assigned this assignment:
1.Generate 10000 positive numbers between (0,200)
2. Search the position of the first 100 in the array
3. Count how many 100 in the array
4. Sort the array
5. Run your program three times and calculate the time:
-to generate 10000 numbers
-to sort the array
-to search the element 100
-to count the 100's
He said we could use any language we choose. I chose Java. I have the Quick Sort method i must use. This is what i have so far, and basically i need assistance in inputing the Quick Sort into what i have already written. THANKS IN ADVANCE!
import java.awt.event.*; import javax.swing.*; import javax.swing.Timer; public class CC { public static void main(String[] args) { int[] List = new int[10000]; int key = 100; int sum = 0; int index = -1; int add = 0; long StartTime = System.currentTimeMillis(); // Creates an array of 10,000 random positive numbers. for (int l=0; l<List.length; l++) { List[l] = (int)(Math.random()*201); } long StopTime = System.currentTimeMillis(); long RunTime = StopTime - StartTime; System.out.println("The time to process an array of 10,000 elements is " +RunTime+ " milliseconds."); StartTime = System.currentTimeMillis(); // Returns the index of the first index containing the number 100 for (int l=0; l<List.length; l++) { if(List[l] == key) { if(index<0) { index = l; System.out.println("The index of the first number 100 in the array is " +l); } } } StopTime = System.currentTimeMillis(); RunTime = StopTime - StartTime; System.out.println("The time to return the index of the first number 100 in an array of 10,000 elements is " +RunTime+ " milliseconds."); StartTime = System.currentTimeMillis(); // Computes the number of times the number of 100 is in the array. for (int l=0; l<List.length; l++) { if(List[l] == key) sum++; } StopTime = System.currentTimeMillis(); RunTime = StopTime - StartTime; System.out.println("The time to return the index of the first number 100 in an array of 10,000 elements is " +RunTime+" milliseconds."); StartTime = System.currentTimeMillis(); // Sorts the array using selection sort. for (int i = List.length-1; i>=1; i--) { int cMax=List[0]; int cMaxIndex=0; for (int j=1; j<=i; j++) { if (cMax<List[j]) { cMax= List[j]; cMaxIndex=j; } } if (cMaxIndex !=i) { List[cMaxIndex] = List [i]; List[i]=cMax; } } /*for (int i=0; i<List.length; i++) { System.out.print(List[i] + " "); }*/ System.out.println("There are " +sum+ " instances of the number 100 in the array."); StopTime = System.currentTimeMillis(); RunTime = StopTime - StartTime; System.out.println("The time to sort an array of 10,000 elements using Selection Sort is " +RunTime+" milliseconds."); } }