package javaapplication1;
import java.util.*;
public class Main
{//Main
static int [] array = new int [10];
static int size,x,y,z;
static int xxx= array[1];
static char c;
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{//main
MainP();
}//main
public static void MainP()
{
boolean continueLoop = true;
do
{
try
{//try
System.out.print("Enter a number [1-10]: \n");
size = scan.nextInt();
continueLoop=false;
}//end try
catch(InputMismatchException inputMismatchException)
{//catch
System.err.printf("\n Exception: %s\n",inputMismatchException);
scan.nextLine();
System.out.println("You must enter a valid integer from 1-10.\nPlease try again.\n");
} //end catch
}//do
while(continueLoop);
y=1;
if(size<=10)
{//if
for(int i=0;i<10;i++,y++)
{//for
try
{
System.out.print("Enter a number "+ y +": \n = ");
array[i]=scan.nextInt();
}//end try
catch(InputMismatchException inputMismatchException)
{//catch
System.err.printf("\n Exception %s\n",inputMismatchException);
scan.nextLine();
System.out.println("You must enter a valid integer.\n Please Try Again. ");
}//end catch
int j=i-1;
z=array[i];
while(j>=0&&z
{
array[j+1]=array[j];
j--;
}
array[j+1]=z;
if(i==size-1)
{
break;
}
}//end for
}//end if
k();
}//MainP
public static void k()
{
System.out.print("Enter a value for K: \n = ");
int k = scan.nextInt();
for(int i=0;i
{
if(xxx
xxx=array[i];
}
System.out.print("The kth largest value is "+xxx+"\n");
try
{
System.out.print("Again? 1=Y 0=N\n");
x=scan.nextInt();
}
catch(InputMismatchException inputMismatchException)
{
System.err.printf("\nException: %s\n", inputMismatchException);
scan.nextLine();
System.out.println("Please enter a valid integer. [1 or 0].");
}
if(x==1)
MainP();
else
System.out.print("Exiting...\n");
System.exit(0);
}
}//Main
7/15/10
Insertion Sort Program
Posted by Aki on 05:07
0 comments:
Post a Comment