Sie sind auf Seite 1von 52

Class Number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

1/1

// Program to check whether number is Even or Odd


import java.util.Scanner;
public class Number
{
public void num()
{
int x;
System.out.print("Enter your number" );
Scanner in= new Scanner(System.in);
x=in.nextInt();
if (x%2==0)
System.out.println("The number is even" );
else
System.out.println("The number is odd" );
}
}

17

Mar 7, 2015 8:20:05 PM

Enter your number 5


The number is odd

Class Tax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

1/1

// Program calculates tax as per user input


import java.io.* ;
public class Tax
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
double cost=0 ;
void tax() throws IOException
{
System.out.println ("Enter the amount of water in Gallons" );
int x = Integer.parseInt(br.readLine());
if (x>30 && x<=40)
{
cost=475;
}
if (x>40 && x<=100)
{
cost=750;
}
if (x>100 && x<=150)
{
cost=1225;
}
if (x>150 && x<=250)
{
cost=1650;
}
if (x>250)
{
cost=2000;
}
System.out.println("The tax is "+cost);
}
}

35
36
37
38
39

Mar 7, 2015 8:20:31 PM

Enter the amount of water in Gallons


1000
The tax is 2000.0

Class Q1_ISC2015
1
2
3
4
5
6
7
8
9
10
11
12
13
14

1/2

import java.util.*;
public class Q1_ISC2015
{
int sumDig(long n) // Function to find sum of digits of a number
{
int sum = 0, d;
while(n>0)
{
d = (int)(n%10);
sum = sum + d;
n = n/10;
}
return sum;
}

15
16

17
18
19
20
21

int countDig(long n) // Function to count the number of digits in a n


umber
{
String s = Long.toString(n);
int len = s.length();
return len;
}

22

public static void main()throws Exception


{
Q1_ISC2015 ob = new Q1_ISC2015();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a value of 'm' from 100 to 10000 : " );
int m = sc.nextInt();
System.out.print("Enter a value of n from 1 to 99 : " );
int n = sc.nextInt();

23
24
25
26
27
28
29
30
31

if(m<100 || m>10000 || n<1 || n>99)


{
System.out.println( "Invalid Input" );
}
else
{
long i = (long)m; // Required number can be out of range of '

32
33
34
35
36
37
38

int'
/* The required number must be greater than 'm',
so loop will go on as long as that n umber is not obtained.

39
40

*/
41
42
43
44
45
46

while(ob.sumDig(i)!=n)
{
i=i+1;
}
System.out.println( "The required number = " +i);
System.out.println( "Total number of digits = " +ob.countDig(i)
Mar 7, 2015 8:21:08 PM

Class Q1_ISC2015 (continued)


46

);
}

47

48
49

2/2

50

Mar 7, 2015 8:21:08 PM

Enter a value of 'm' from 100 to 10000 : 6000


Enter a value of n from 1 to 99 : 42
The required number = 69999
Total number of digits = 5

Class Magic
1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

1/2

//Program to check whether a number is a magic number or not.


import java.io.* ;
public class Magic
{
int n; //"This." part is not necessary. Helps unnderstand better
public Magic()
{
this.n = 0; // The this keyword refers to the currently calling o
bject.
}
void getnum(int nn)
{
this.n=nn ;
}
int Sum_of_digits(int a)
{
int sum=0;
while(a>0)
{
sum+=a%10;
a=a/10 ;
}
if (sum >= 10)
{
return Sum_of_digits(sum); //Recursive Case
}
else
{
return sum ; //Base Case
}
}
void ismagic() //Method displays whether number is a magic number or
not.
{
if(Sum_of_digits(this.n)==1)
{
System.out.println("It is a magic number" );
}
else
{
System.out.print("Not a magic number!" );
}
}
InputStreamReader isr = new InputStreamReader(System.in) ;
BufferedReader br = new BufferedReader(isr);
public void main(String[]args) throws IOException
{
System.out.println("Enter your number" );
int n= Integer.parseInt(br.readLine());
Mar 7, 2015 8:21:50 PM

Class Magic (continued)


Magic m = new Magic(); // Creating object
m.getnum(n) ;
m.ismagic();

48
49
50

51
52

2/2

53
54

Mar 7, 2015 8:21:50 PM

Enter your number


153
Not a magic number!

Class RotateArray
1
2
3
4
5
6
7
8
9
10

1/2

//Program to Rotate Array by 90 degrees clockwise


import java.io.* ;
import java.util.* ;
public class RotateArray
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : " );
int m=sc.nextInt();

11
12
13
14
15
16

if(m<3 || m>9)
System.out.println("Size Out Of Range" );
else
{
int A[][]=new int[m][m];

17
18
19
20
21
22
23
24
25
26

/* Inputting the matrix */


for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print( "Enter an element : " );
A[i][j]=sc.nextInt();
}
}

27
28
29
30
31
32
33
34
35
36
37
38
39

/* Printing the original matrix */


System.out.println("*************************" );
System.out.println("The Original Matrix is : " );
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();
}
System.out.println("*************************" );

40
41
42
43
44
45
46
47
48
49

/*Rotation of matrix begins here */


System.out.println("Matrix After Rotation is : " );
for(int i=0;i<m;i++)
{
for(int j=m-1;j>=0;j--)
{
System.out.print(A[j][i]+ "\t");
}
System.out.println();
Mar 7, 2015 8:22:34 PM

Class RotateArray (continued)


50
51

2/2

}
System.out.println("*************************" );

52
53

54
55
56
57

int sum = A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1]; // Finding


sum of corner elements
System.out.println("Sum of the corner elements = " +sum);
}
}
}

Mar 7, 2015 8:22:34 PM

Enter the size of the matrix : 4


Enter an element : 1
Enter an element : 2
Enter an element : 3
Enter an element : 4
Enter an element : 5
Enter an element : 6
Enter an element : 7
Enter an element : 8
Enter an element : 9
Enter an element : 10
Enter an element : 11
Enter an element : 12
Enter an element : 13
Enter an element : 14
Enter an element : 15
Enter an element : 16
*************************
The Original Matrix is :
1

10

11

12

13

14

15

16

*************************
Matrix After Rotation is :
13

14

10

15

11

16

12

*************************
Sum of the corner elements = 34

Class Prime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

1/1

// To check whether the number is prime or not


import java.io.* ;
public class Prime
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr) ;
public void main(String[]args)throws IOException
{
System.out.println("Enter your number" );
int n= Integer.parseInt(br.readLine());
int temp=0;
for(int j=2; j<n; j++)
{
if (n/j==0)
temp=1 ;
}
if (temp==0)
System.out.print("The number is prime" );
else
System.out.print("The number is not prime" );
}
}

23
24

Mar 7, 2015 8:23:23 PM

Enter your number


97
The number is prime

Class Pattern2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

1/1

//Program to display a type of pattern


import java.io.* ;
public class Pattern2
{
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
public void main(String[]args) throws IOException
{
System.out.println("Enter the max number of a's " );
int n= Integer.parseInt(br.readLine());
for(int a=1;a<=n;a++)
{
for(int j=n-a;j>0;j--)
System.out.print(" ");
for(int r=1;r<=a;r++)
System.out.print("a ");
System.out.println();
}
for(int a=n-1;a>=1;a--)
{
for(int j=n-a;j>0;j--)
System.out.print(" ");
for(int r=a;r>=1;r--)
System.out.print("a ");
System.out.println();
}
}
}

29
30

Mar 7, 2015 8:24:06 PM

Enter the max number of a's


10
a
a a
a a a
a a a a
a a a a a
a a a a a a
a a a a a a a
a a a a a a a a
a a a a a a a a a
a a a a a a a a a a
a a a a a a a a a
a a a a a a a a
a a a a a a a
a a a a a a
a a a a a
a a a a
a a a
a a
a

Class Pattern3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

1/1

//Program to display a certain type of pattern


import java.io.* ;
public class Pattern3
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr) ;
public void main(String[]args)throws IOException
{
System.out.println("Enter the max number of line's " );
int n= Integer.parseInt(br.readLine());
for(int a=1;a<=n;a++)
{
for(int j=n-a;j>0;j--)
System.out.print(" ");
for(int r=1;r<=a;r++)
System.out.print(r);
System.out.println();
}
for(int a=n-1;a>=1;a--)
{
for(int j=n-a;j>0;j--)
System.out.print(" ");
for(int r=a;r>=1;r--)
System.out.print(r);
System.out.println();
}
}
}

29
30

Mar 7, 2015 8:24:49 PM

Enter the max number of line's


10
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
987654321
87654321
7654321
654321
54321
4321
321
21
1

Class LinearSearch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

1/1

//Program to showcase Linear Search


import java.util.Scanner ;
public class LinearSearch
{
Scanner in=new Scanner(System.in);
public void main(String[]args)
{
System.out.println("Enter the number of elements in the array" );
int n= in.nextInt() ;
int array[] = new int[n];
int i=0;
for(i=0; i<n ;i++)
{
array[i]=in.nextInt();
}
System.out.println("Enter the number you want to search for" );
int a = in.nextInt() ;
for(i=0; i<n ; i++)
{
if(a==array[i])
break ;
}
System.out.println("The element was found at " +(i+1));
}
}

26
27
28

Mar 7, 2015 8:25:14 PM

Enter the number of elements in the array


8
12
41234
1234
21
78
256
69
4
Enter the number you want to search for
69
The element was found at 7

Class Factorial
1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16

17
18

1/1

//Program to print factorial of a number


public class Factorial
{
public void factorial(int num)
{
int fact=1 ;
if (num>0)
{
for(; num>0 ; num--)
{
fact*=num ;
}
System.out.println(" The factorial of the number you entered is "
+fact) ;
}
else if (num <=0)
System.out.println(" The factorial of the n umber you entered is n
ot defined! ");
}
}

19
20

Mar 7, 2015 8:27:10 PM

Class Birthday
1
2
3
4

1/2

//Program to showcase the Birthday Paradox


import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;

5
6
7

public class Birthday


{

8
9
10
11
12
13
14
15
16
17
18
19
20
21

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
Birthday a = new Birthday();
long t = System.currentTimeMillis();
System.out.print("Enter no of people: " );
int no = sc.nextInt();
System.out.print("Enter no of times to be run: " );
long accuracy= sc.nextLong();
a.calc(no, accuracy);
System.out.println("time = " + (System.currentTimeMillis() - t));
sc.close();
}

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

38

public void calc(int no, long accuracy)


{
Random r = new Random();
long found = 0, total = 0;
short[] people = new short[no];
for (long i = 0; i < accuracy; i++)
{
for (int j = 0; j < people.length; j++)
{
people[j] = (short) r.nextInt(365);
}
if (check(people)) found++;
total++;
}
System.out.println("Percentage = " + (double)found/(double)total * 10
0 +"%" + "\nfound =" + found + "\ntotal = " + total);
}

39
40
41
42
43
44
45
46
47
48

public boolean check(short[] a)


{
Arrays.sort(a);
for (int i = 0; i < a.length; i++)
{
if (i == a.length - 1) break;
if (a[i] == a[i + 1]) return true;
}
return false;
Mar 7, 2015 8:28:36 PM

Class Birthday (continued)


}

49
50

2/2

51

Mar 7, 2015 8:28:36 PM

Enter no of people: 38
Enter no of times to be run: 1000
Percentage = 87.0%
found =870
total = 1000
time = 8062

Class Diamond1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

1/1

//Program to print the Diamond Pattern


import java.io.* ;
public class Diamond1
{
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader(isr);
public void main(String[]args) throws IOException
{
System.out.println("Enter the number of lines" ) ;
int n = Integer.parseInt(br.readLine());
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}

21

for (int i = n - 1; i >= 1; i--)


{
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
{
System.out.print("*");
}
System.out.println();

22
23
24
25
26
27
28
29
30
31
32

33

34
35

36
37

Mar 7, 2015 8:30:21 PM

Enter the number of lines


10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*

Class palindrome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

1/1

//Program to check whther the number is a palindrome or not.


import java.io.* ;
public class palindrome
{
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader(isr);
public void main(String[]Args) throws IOException
{
System.out.println("Enter the number") ;
int num = Integer.parseInt(br.readLine());
int n = num;
int k; int rev=0;
while(num>0)
{
k=num%10 ;
rev=(rev*10) + k ;
num=num/10 ;
}
if (rev == n)
System.out.print("The number" +n+ "is a palindrome!") ;
else
System.out.print("The number " +n+ " is NOT a palindrome.") ;
}
}

25
26
27

Mar 7, 2015 8:31:01 PM

Enter the number


121
The number121is a palindrome!

Class SumOfPrimes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

26
27

1/1

//Program to print sum of primes till a certain limit defined by the user
import java.io.* ;
public class SumOfPrimes
{
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader(isr);
public void main(String[]args) throws IOException
{
System.out.println("Enter the maximum limit" ) ;
int n = Integer.parseInt(br.readLine());
int sum=0; int flag=0;
for(int i=2 ; i<=n ; i++)
{
for(int j=2 ; j<i ; j++)
{
if(i%j==0)
flag = 1 ;
}
if(flag==0)
{
sum=sum+i;
}
flag=0;
}
System.out.print("The Sum of prime numbers till " +n+ " is : " +s
um);
}
}

28

Mar 7, 2015 8:32:19 PM

Enter the maximum limit


100
The Sum of prime numbers till 100 is : 1060

Class SwitchCase
1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

1/1

/* Program to Implement Switch Case and Print the day as per user choice.
*/
public class SwitchCase
{
public void Switch(int day)
{
String ans ;
switch(day)
{
case 1: ans="Sunday" ;
break; // Break is used here to avoid "fallthrough"
case 2: ans="Monday" ;
break;
case 3: ans="Tuesday" ;
break;
case 4: ans="Wednesday" ;
break;
case 5: ans="Thursday" ;
break;
case 6: ans="Friday" ;
break;
case 7: ans="Saturday" ;
break;
default: ans="Wrong Day Number!" ;
break;
}
System.out.println(ans);
}
}

Mar 7, 2015 8:33:37 PM

Class ImageMatrix_ISC2013
1
2
3
4
5
6
7
8

1/2

import java.io.*;
public class ImageMatrix_ISC2013
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of the square matrix : " );
int m=Integer.parseInt(br.readLine());

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

if(m>2 && m<20) //checking given condition


{
int A[][]=new int[m][m];
System.out.println("Enter the elements of the Matrix : " );
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
A[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("*********************" );
System.out.println("The original matrix:" );
System.out.println("*********************" );
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}

32
33
34
35
36
37
38
39
40
41
42
43

// creating the Image Matrix


int B[][]=new int[m][m];
for(int i=0;i<m;i++)
{
int k=0;
for(int j=m-1;j>=0;j--)
{
B[i][k]=A[i][j];
k++;
}
}

44
45
46
47
48
49

//Printing both the Matrix


System.out.println("*********************" );
System.out.println("The Mirror Image:");
System.out.println("*********************" );
for(int i=0;i<m;i++)
Mar 7, 2015 8:35:13 PM

Class ImageMatrix_ISC2013 (continued)


50
51
52
53
54
55
56
57
58
59
60
61

2/2

{
for(int j=0;j<m;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
}
else
System.out.println("Output : Size Out Of Range" );
}
}

Mar 7, 2015 8:35:13 PM

Enter the size of the square matrix : 4


Enter the elements of the Matrix :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
*********************
The original matrix:
*********************
1

10

11

12

13

14

15

16

*********************
The Mirror Image:
*********************
4

12

11

10

16

15

14

13

Class SortSearch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

48

1/2

/* Java program to sort numbers and search for a number */


import java.io.* ;
import java.util.* ;
public class SortSearch
{
Scanner in = new Scanner(System.in);
public void main(String[]args) throws IOException
{
int n ; int swap ; int i ;
int c, first, last, middle, search;
System.out.println("Enter the number of elements in the array" ) ;
n= in.nextInt();
int arr[] = new int[n];
System.out.println("Enter the elements of the array" ) ;
for(i =0 ; i<n ; i++)
{
arr[i] = in.nextInt() ;
}
int temp;
for (i = 1; i < n; i++)
{
for(int j = i ; j > 0 ; j--)
{
if(arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
}
System.out.println("Sorted list of numbers" );
for (i = 0; i < n; i++)
System.out.println(arr[i]);
System.out.println() ;
System.out.println("Enter value to find" );
search = in.nextInt();
first = 0;
last
= n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( arr[middle] < search )
first = middle + 1;
else if ( arr[middle] == search )
{
System.out.println(search + " found at location " + (midd
le + 1) + ".");
break;
Mar 7, 2015 8:35:58 PM

Class SortSearch (continued)


}
else
last = middle - 1;
middle = (first + last)/2;

49
50
51
52

}
if ( first > last )
System.out.println(search + " is not present in the list." );

53
54
55

56
57

2/2

58

Mar 7, 2015 8:35:58 PM

Enter the number of elements in the array


7
Enter the elements of the array
1
2
3
29
2132
31
632
Sorted list of numbers
1
2
3
29
31
632
2132

Enter value to find


31
31 found at location 5.

Class HCF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

1/1

//Program to print HCF of a number


import java.io.* ;
import java.util.* ;
public class HCF
{
Scanner in = new Scanner(System.in);
public void run()
{
int result = GCD(24,9);
System.out.println(result) ;
}
public int GCD(int a , int b)
{
if (b==0)
return a ; //base case
else
return GCD(b, a%b) ; //recursive case
}
public static void main(String[]args) throws IOException
{
HCF GCD = new HCF() ;
GCD.run() ; // Calling method run from object created
}
}

25
26

Mar 7, 2015 8:37:24 PM

The GCD of 24 and 9 is: 3

Class FactorialWithRecursion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

1/1

/* Program to print Factorial of a number using Recursion */


import java.io.* ;
import java.util.* ;
public class FactorialWithRecursion
{
Scanner in = new Scanner(System.in) ;
public void run()
{
int result = fact(6);
System.out.println(result);
}
public int fact(int n)
{
if (n==0)
{
return 1; // Base Case
}
else
{
return n*fact(n-1) ; // Recursive case: Method calls itself
}
}
public static void main(String[]args) throws IOException
{
FactorialWithRecursion factorial = new FactorialWithRecursion() ;
factorial.run() ;
}
}

29
30
31
32
33
34

Mar 7, 2015 8:39:36 PM

The factorial of 6 is : 720

Class FibWithRecursion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

1/1

//Program to calculate the fibionacci of a number using recursion


import java.io.* ;
import java.util.* ;
public class FibWithRecursion
{
Scanner in = new Scanner(System.in) ;
public void run()
{
int result = fib(6);
System.out.println(result);
}
public int fib(int a) //Calculates the fibionacci of a number
{
if (a==1)
return 0; //Base case - 1
else if(a==2)
return 1; // Base case - 2
else
return fib(a-1) + fib(a-2) ; // Recursive case
}
public static void main(String[]args) throws IOException
{
FibWithRecursion obj1 = new FibWithRecursion();
obj1.run() ;// Calling method Run from object created
}
}

27

Mar 7, 2015 8:40:30 PM

The fibionacci of 6 is : 5

Class BinarySearch
1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

31
32
33
34
35
36
37
38
39
40

1/1

//Program to carry out Binary Search


import java.io.* ;
import java.util.*;
public class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements" );
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers. BEWARE! Array MUST BE S
ORTED IN ASCENDING ORDER.");
//Needs a SORTED ARRAY
for (c = 0; c < n; c++)
{
array[c] = in.nextInt(); // Array elements are inputed
}
System.out.println("Enter value to find");
search = in.nextInt();
first = 0;
last
= n - 1;
middle = (first + last)/2;
while( first <= last ) // Test condition is first <= last
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + " found at location " + (middle + 1)
+ ".");
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
System.out.println(search + " is not present in the list." );
}
}

41
42

Mar 7, 2015 8:41:16 PM

Enter number of elements


8
Enter 8 integers. BEWARE! Array MUST BE SORTED IN ASCENDING ORDER.
1
2
3
4
5
6
7
8
Enter value to find
4
4 found at location 4.

Class BubbleSort
1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
17
18
19
20
21
22

23
24
25
26
27
28
29

1/1

//Program to sort numbers using the Bubble sort technique


import java.io.* ;
import java.util.Scanner ;
public class BubbleSort
{
Scanner in = new Scanner(System.in);
public void main(String[]args) throws IOException
{
int n ; int swap ; int i ; // Swap is a temporary variable,used f
or swapping.
System.out.println("Enter the number of elements in the array" ) ;
n= in.nextInt();
int array[] = new int[n];
System.out.println("Enter the elements of the array" ) ;
for(i =0 ; i<n ; i++)
{
array[i] = in.nextInt() ; //Array elements are inputed
}
for (i =0 ; i<n ; i++)
{
for (int d =0 ; d<n-i-1; d++)
{
if (array[d] > array[d+1]) /* For descending order use <
*/
{
swap
= array[d];
array[d]
= array[d+1];
array[d+1] = swap;
} //Swapping is carried out
}
}

30

System.out.println("Sorted list of numbers" );


for (i = 0; i < n; i++)
System.out.println(array[i]);

31
32
33

34
35

36
37
38
39
40

Mar 7, 2015 8:41:57 PM

Enter the number of elements in the array


7
Enter the elements of the array
3721
3421
432
2
6
9867
35
Sorted list of numbers
2
6
35
432
3421
3721
9867

Class SelectionSort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

1/1

//Program to carry out sorting of numbers using selection sort


import java.io.* ;
import java.util.* ;
public class SelectionSort
{
Scanner in = new Scanner(System.in);
public void main(String[]args) throws IOException
{
int n ; int swap ; int i ;
System.out.println("Enter the number of elements in the array" ) ;
n= in.nextInt();
int arr[] = new int[n];
System.out.println("Enter the elements of the array" ) ;
for(i =0 ; i<n ; i++)
{
arr[i] = in.nextInt() ; //Array elements are inputted
}
for (i = 0; i < n-1; i++)
{
int index = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[index])
index = j;
}

26
27
28
29

30
31
32
33
34
35

int temp = arr[index];


arr[index] = arr[i];
arr[i] = temp; //Swapping is carried out with temporary varia
ble temp.
}
System.out.println("Sorted list of numbers");
for (i = 0; i < n; i++)
System.out.println(arr[i]);
}
}

Mar 7, 2015 8:42:41 PM

Enter the number of elements in the array


7
Enter the elements of the array
323
32
567
2
969
3
99
Sorted list of numbers
2
3
32
99
323
567
969

Class InsertionSort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

32
33
34
35

1/1

//Program to carry out sorting of numbers using Insertion Sort technique


import java.io.* ;
import java.util.* ;
public class InsertionSort
{
Scanner in = new Scanner(System.in);
public void main(String[]args) throws IOException
{
int n ; int swap ; int i ;
System.out.println("Enter the number of elements in the array" ) ;
n= in.nextInt();
int arr[] = new int[n];
System.out.println("Enter the elements of the array" ) ;
for(i =0 ; i<n ; i++)
{
arr[i] = in.nextInt() ; // Array elements are inputted
}
int temp;
for (i = 1; i < n; i++)
{
for(int j = i ; j > 0 ; j--)
{
if(arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp; //Swapping is carried out
}
}
}
System.out.println("Sorted list of numbers"); //Prints sorted lis
t of numbers
for (i = 0; i < n; i++)
System.out.println(arr[i]);
}
}

36
37

Mar 7, 2015 8:43:26 PM

Enter the number of elements in the array


8
Enter the elements of the array
2313
52
8
0
3343
31
17
3
Sorted list of numbers
0
3
8
17
31
52
2313
3343

Das könnte Ihnen auch gefallen