Вы находитесь на странице: 1из 6

AP Computer Science Program

import java.util.*;

public class Sorting


{
static Scanner input = new Scanner(System.in );
static Random random = new Random();
static Timer timer;
static int size, times, max;
static int[] intarray;
static Boolean sorted = false;

//**************************************************************

public static int linearSearch(int[] intarray)


{
int findint, found = -1;

System.out .print("Enter a value to search the array for: ");


findint = input.nextInt();
long starttime = System.nanoTime();

for(int x = 0; x < size; x++)


{
if (sorted) {
if (intarray[x] == findint) {
found = x + 1;
x = size;
}
}
else if (intarray[x] == findint) {found = x + 1; x = size;}
}

long endtime = System.nanoTime();


long timetotal = (long)(endtime - starttime);
long seconds = (int)timetotal/1000000000;
timetotal = timetotal - (seconds * 1000000000);
System.out .println("Searching took " + seconds + "s, " +
timetotal/1000000.0 + " ms.");
return found;
} //end of linearSearch()

//**************************************************************

public static void printArray(int[] intarray)


{
int y = 0;
System.out .println();
for(int x = 0; x < size; x++)
{
int z = intarray[x];
System.out .print(z + "\t");
y++;
if(y == 10) {System.out .println(); y = 0;}
} //end of for loop
} //end of printArray()

//**************************************************************

public static void selectionSort(int[] intarray)


{
long starttime = System.nanoTime();
System.out .println("Sorting...");
int x = 0, y, z, temp;
int oneten, onehun, mten = 9, mhun = 99;
Boolean ten = false, hun = false;

if (size > 49000 && size <= 149999) ten = true;


if (size > 149999) hun = true;
z = size - 1;
y = 0;
oneten = (size - 1)/10;
onehun = (size - 1)/100;

while (!sorted)
{
for (x = 0; x <= z; x++)
if (intarray[x] > intarray[y]) y = x;
temp = intarray[z];
//final value is copied
intarray[z] = intarray[y];
//largest array value copied to final spot in array
intarray[y] = temp;
z--;
y = 0;
if (z == 0) sorted = true;
if (oneten * mten == z && ten) {
System.out .println(100 - (mten*10) + "%");
mten--;}
if (onehun * mhun == z && hun) {
System.out .println(100 - (mhun) + "%");
mhun--;}
}
long endtime = System.nanoTime();
long timetotal = (long)(endtime - starttime);
long seconds = timetotal/1000000000;
timetotal = timetotal - (seconds * 1000000000);
System.out .println("Sorting took " + seconds + "s, " +
timetotal/1000000.0 + " ms.");
} //end of selectionSort()
//**************************************************************

public static int binarySearch(int[] intarray)


{
int findint, found = -1, high = size - 1, low = 0, mid = high/2,
comparisons = 0;

if (!sorted) found = -3;


else {
System.out .print("Enter a value to search the array for: ");
findint = input.nextInt();

long starttime = System.nanoTime();


if (findint > intarray[size - 1]) found = -2;
while (found == -1)
{
if (findint < intarray[mid]) {
high = mid;
mid = ((high - low)/2) + low;
comparisons++;}
if (findint > intarray[mid]) {
low = mid;
mid = ((high - low)/2) + low;
comparisons++;}
if (findint == intarray[mid]) {
comparisons++;
while (found == -1)
{ if (mid != 1)
{
if (intarray[mid] == intarray[mid-1]) mid--;
else found = mid + 1;
}
else
{
if (intarray[0] == intarray[1]) found = 1;
else found = 2;
}
} //while(found == -1)
} //if(findint ==intarray[mid])

if (high - low <= 1) {


if (intarray[high] == findint) found = high;
if (intarray[low] == findint) found = low;
else found = -2;
comparisons++;
}

} // end of while

long endtime = System.nanoTime();


long timetotal = (long)(endtime - starttime);
long seconds = (int)timetotal/1000000000;
timetotal = timetotal - (seconds * 1000000000);
System.out .println("\nSearching took " + seconds + "s, " +
timetotal/1000000.0 + " ms.");
System.out .println(comparisons + " numbers were checked.");
} //end of else
return found;
} //end of binarySearch()

//**************************************************************

public static void reload(int[] intarray)


{
System.out .println("Enter the max value of integers to load into
array");
System.out .println("(The size cannot be changed)");
max = input.nextInt() + 1;
for (int x = 0; x < size; x++){
int y = random.nextInt(max);
intarray[x] = y;
} //end of for loop
}

//**************************************************************

public static int menu()


{
int select = 1;

System.out .println("\nLinear Search ----- 1");


System.out .println("Print Array ------- 2");
System.out .println("Selection Sort ---- 3");
System.out .println("Binary Search ----- 4");
System.out .println("Reload Array ------ 5");
System.out .println("Quit -------------- 0");
System.out .print("\nEnter selection: ");
select = input.nextInt();
return select;
} //end of menu()

//**************************************************************

public static void main(String[] args)


{
int location, select;
Boolean done = false;
System.out .println("Enter array size:");
size = input.nextInt();
int[] intarray = new int[size];

reload(intarray);

while(!done)
{
select = menu();
switch (select) {
//**************************************************** LINEAR SEARCH RESULTS
case 1:
location = linearSearch(intarray);
if (location != -1)
{
if (sorted)
{
if (location > 0)
{
System.out .print("\nInteger found at the
following location(s): " + location);
if (intarray[location - 1] == intarray[size - 1])
{
for(int x = location; x < size; x++) {
location++;
System.out .print(", " + location);
}
} //end of if

else {
for(int z = location - 1; intarray[z] == intarray[z+1];z++)
{
location++;
System.out .print(", " + location);
}
} //end of else
System.out .println();
}//if(location > 0)
} //if (sorted)

else {
System.out .println("\nInteger first found at
location " + location + " in the unsorted
array. ");
System.out .println("The integer may exist in other
locations.");
} //else
} //if (location != 1)

else System.out .println("\nInteger not found in the


array.");
break;
//*****************************************************************************
case 2:
printArray(intarray);
break;
case 3:
selectionSort(intarray);
break;
//******************************************************* BINARY SEARCH RESULTS
case 4:
location = binarySearch(intarray);
if (location == -3) System.out .println("The array has not
been sorted yet. Sort array before using Binary
Search.");
if (location == -2) System.out .println("The value is not in
the array.");
if (location > 0) {
System.out .print("The integer was found at the
following location(s): " + location);

if (intarray[location - 1] == intarray[size - 1])


{
for(int x = location; x < size; x++) {
location++;
System.out .print(", " + location);
}
} //end of if

else {
for(int z = location - 1; intarray[z] == intarray[z+1];z++)
{
location++;
System.out .print(", " + location);
}
} //end of else
System.out .println();
}
break;
//*****************************************************************************
case 5:
reload(intarray);
break;
case 0:
done = true;
break;
default:
System.out .println("Invalid Entry");
break;
} //end of switch case
} //end of while loop
} //end of main
} //end of class

Вам также может понравиться