2. This is in java. (50 points) Sorting Create 3 text files, one with the 50000
ID: 3888417 • Letter: 2
Question
2. This is in java. (50 points) Sorting Create 3 text files, one with the 50000 integers 1 through 50000 in order, one with the 50000 integers 1 through 50000 in reverse order, and one with 50000 random integers (between 1 and 50000). Write a program to
Read the name of a file
Store the contents of the file into a 50000 element array,
Sort the array using an Insertion Sort, and
Print the actual running time (without any server overhead) of the sort.
Print the number of comparisons used in the sort.
Print just the first 30 values in the resulting array
Explanation / Answer
}
}
---------------------------------------
//with random order
//Read this file : CheggTextFile.txt
// To stored in Array:
}
---------------------
//Sort array using insertion sort
public class InsertionSort
{
public static void main(String a[])
{
int[] arrayTwo = InsertionSort(integers);
for(int i:arrayTwo){
System.out.print(i);
System.out.print(", ");
}
}
public static int[] InsertionSort(int[] input){
int temp;
for (int i = 1; i < input.length; i++) {
for(int j = i ; j > 0 ; j--){
if(input[j] < input[j-1]){
temp = input[j];
input[j] = input[j-1];
input[j-1] = temp;
}
}
}
return input;
}
}
----------------------------------------
// time count ,
long length = 0;
const long max_length = 50000;
int list[max_length];
void readcount()
{
ifstream fin("random.dat", ios::binary);
for (long i = 0; i < length; i++)
{
fin.read((char*)&list[i], sizeof(int));
}
fin.close();
}
int main()
{
double t1,t2;
for (length = 50000; length <= max_length; )
{
readcount();
t1 = clock();
InsertionSort();
t2 = clock();
cout << "Insertion Sort : " << (t2 - t1)/CLK_TCK << " sec ";
switch (length)
{
case 1000 :
length = 5000;
break;
case 5000 :
length = 10000;
break;
case 10000 :
length = 50000;
break;
}
}
return 0;
}
-------------
// You can compare count
------------------
//Print Just 30 values
--------- in similar way you can define text file for all cases------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.