File tree Expand file tree Collapse file tree 9 files changed +228
-0
lines changed
Sorting Algorithm in java Expand file tree Collapse file tree 9 files changed +228
-0
lines changed Original file line number Diff line number Diff line change 1+ public class BubbleSort {
2+ public static void main (String [] args ) {
3+ int arr []={4 ,1 ,3 ,9 ,7 };
4+ int n =arr .length ;
5+ for (int i =0 ;i <n ;i ++){
6+ int k =n ;
7+ for (int j =0 ;j <k -1 ;j ++){
8+ if (arr [j ]>arr [j +1 ]){
9+ int temp =arr [j ];
10+ arr [j ]=arr [j +1 ];
11+ arr [j +1 ]=temp ;
12+ }
13+ }
14+ k --;
15+
16+ }
17+ for (int i =0 ;i <n ;i ++)
18+ System .out .println (arr [i ]);
19+
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ public class InsertionSort {
2+ public static void main (String [] args ) {
3+ int a []={3 ,5 ,2 ,4 ,1 };
4+ for (int i =1 ;i <a .length ;i ++){
5+ int temp =a [i ];
6+ int j =i -1 ;
7+ while (j >=0 && temp <a [j ]){
8+ a [j +1 ]=a [j ];
9+ j --;
10+ }
11+ a [j +1 ]=temp ;
12+ }
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ public class SelectionSort {
2+ public static void main (String args []) {
3+ int a []={3 ,5 ,2 ,4 ,1 };
4+ for (int i =0 ;i <a .length -1 ;i ++){
5+ for (int j =i +1 ;j <a .length ;j ++){
6+ if (a [i ]>a [j ]){
7+ int temp =a [i ];
8+ a [i ]=a [j ];
9+ a [j ]=temp ;
10+ }
11+ }
12+ }
13+ for (int i =0 ;i <a .length ;i ++)
14+ System .out .println (a [i ]);
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class ChocolateVanilla {
4+ public static void main (String [] args ) {
5+ Scanner sc = new Scanner (System .in );
6+ long n =sc .nextLong (),k =sc .nextLong ();
7+ Long c = n %2 ==0 ? (n /2 ) : (n /2 )+1 ;
8+ if (k <=c )
9+ System .out .println ((k *2 )-1 );
10+ else
11+ System .out .println ((k -c )*2 );
12+ sc .close ();
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ public class GroupOfPrimeNumber {
2+ public static boolean isprime (long r ){
3+ if (r !=2 && r %2 ==0 ) return false ;
4+ for (int i =2 ;i <Math .sqrt (r );i ++){
5+ if (r %i ==0 )
6+ return false ;
7+ }
8+ return true ;
9+ }
10+ public static void main (String args []) {
11+ long a =1513101918 ;
12+ int n =2 ;
13+ // int d=(int)Math.pow(10,n);
14+ // while(a>0){
15+ // long r=a%d;
16+ // if(isprime(r))
17+ // System.out.println(r);
18+ // a/=d;
19+ // }
20+ String s =String .valueOf (a );
21+ int i =0 ;
22+ while (i !=s .length ()){
23+ int r =Integer .parseInt (s .substring (i ,i +n ));
24+ if (isprime (r ))
25+ System .out .println (r );
26+ i +=n ;
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ class Node {
4+ public int val ;
5+ public Node next ;
6+ }
7+ public class LinkedList {
8+ static Scanner sc =new Scanner (System .in );
9+ static Node head ;
10+
11+
12+ void createNode (){
13+ System .out .println ("Enter the insert value :" );
14+ int val =sc .nextInt ();
15+ Node obj1 =new Node ();
16+ obj1 .val =val ;
17+ obj1 .next =null ;
18+
19+
20+ if (head ==null ){
21+ System .out .println ("head is null" );
22+ head =obj1 ;
23+ }
24+ else {
25+ Node p =head ;
26+ while (p .next !=null ){
27+ System .out .println (p .val );
28+ p =p .next ;
29+ }
30+ p .next =obj1 ;
31+ }
32+ }
33+
34+ void display (){
35+ System .out .println ("Display the node list :" );
36+ Node p =head ;
37+ while (p !=null ){
38+ System .out .println (p .val );
39+ p =p .next ;
40+ }
41+
42+ }
43+ public static void main (String [] args ) {
44+
45+ LinkedList ob =new LinkedList ();
46+ int n ;
47+ do {
48+ System .out .println ("Enter the operation :" );
49+ n =sc .nextInt ();
50+ switch (n ){
51+ case 1 :
52+ ob .createNode ();
53+ break ;
54+ case 2 :
55+ ob .display ();
56+ break ;
57+ default :
58+ System .out .println ("Wrong operation" );
59+ }
60+
61+ }while (n <=2 );
62+
63+
64+ }
65+ }
Original file line number Diff line number Diff line change 1+ public class checkingMainMethod {
2+ public static void main (String args []) {
3+ System .out .println (args );
4+ }
5+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ interface area {
3+ public void rect ();
4+ public void circle ();
5+ }
6+
7+ public class oops implements area
8+ {
9+ int a =10 ;
10+ int b =20 ;
11+ int r =2 ;
12+ public void rect (){
13+ System .out .println ("Area of rectangle is 10*20: " +(a *b ));
14+ }
15+ public void circle (){
16+ System .out .println ("Area of circle is 2*2*3.14: " +(r *r *3.14 ));
17+ }
18+
19+ public static void main (String args [])
20+ {
21+ Scanner sc =new Scanner (System .in );
22+ int s =sc .nextInt ();
23+ System .out .println (s );
24+ oops obj =new oops ();
25+ obj .rect ();
26+ obj .circle ();
27+ System .out .println ("Hello World" );
28+ sc .close ();
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class primeNumber {
4+
5+ public static boolean isprime (int n ){
6+ // if(n==0 || n==1)
7+ // return false;
8+ // if(n==2)
9+ // return true;
10+ // for(int i=2;i<n/2;i++){
11+ // if(n%i==0)
12+ // return false;
13+ // }
14+ // return true;
15+
16+ if (n !=2 && n %2 ==0 )
17+ return false ;
18+ for (int i =2 ;i <=Math .sqrt (n );i ++){ // algorithm
19+ if (n %i ==0 )
20+ return false ;
21+ }
22+
23+ return true ;
24+ }
25+ public static void main (String [] args ) {
26+ Scanner sc =new Scanner (System .in );
27+ int n =sc .nextInt ();
28+ if (isprime (n ))
29+ System .out .println ("Prime number" );
30+ else
31+ System .out .println ("not a prime number" );
32+ sc .close ();
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments