KEMBAR78
Some Programs | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
10 views9 pages

Some Programs

Uploaded by

sidhuprasad242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Some Programs

Uploaded by

sidhuprasad242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

import java.util.

*;
class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] m = new int[10];

// Taking input
System.out.println("Enter 10 numbers:");
for (int i = 0; i < 10; i++) {
m[i] = sc.nextInt();
}

// Correct Bubble Sort


for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9 - i; j++) {
if (m[j] > m[j + 1]) {
int temp = m[j];
m[j] = m[j + 1];
m[j + 1] = temp;
}
}
}

// Display sorted array


System.out.println("Sorted array in ascending order:");
for (int i = 0; i < 10; i++) {
System.out.print(m[i] + " ");
}
}
}

import java.util.*;
class SelectionSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] m = new int[10];
int min,t;

// Taking input
System.out.println("Enter 10 numbers:");
for (int i = 0; i < 10; i++) {
m[i] = sc.nextInt();
}

for (int i = 0; i < 9; i++) {


min=i;
for (int j = i+1; j < 10; j++) {
if (m[j] < m[min])
min=j;

}
t = m[i];
m[i] = m[min];
m[min]=t;
}

// Display sorted array


System.out.println("Sorted array in ascending order:");
for (int i = 0; i < 10; i++) {
System.out.print(m[i] + " ");
}
}
}

import java. util. Scanner;


class linear
{
public static void main (String arg[])
{
Scanner Sc=new Scanner(System.in);
int i,ns,flag=0;
int m[]=new int[10];
System.out.println("Enter 10 elements");
for(i=0;i<10;i++)
{
m[i]=Sc.nextInt() ;
}
System.out.println("Enter the no to be searched");
ns=Sc.nextInt() ;
for(i=0;i<10;i++)
{
if(m[i]==ns)
flag=1;
}
if(flag==1)
{
System.out.println("Number found ");

}
else
{
System.out.println("no not found");

}
}

Import java. util. Scanner;


class linear
{
public static void main (String arg[])
{
Scanner Sc=new Scanner(System.in);
int mid, i,ns,flag=0,lb=0,ub=9;
int m[]=new int[10];
System.out.println("Enter 10 elements");
for(i=0;i<10;i++)
{
m[i]=Sc.nextInt() ;
}
System.out.println("Enter the no to be searched");
ns=Sc.nextInt() ;
while(lb<=ub)
{
mid=(lb+ub)/2;
if(ns>m[mid])
{
lb=mid+1;
}
if(ns<m[mid])
{
ub=mid-1;
}
if(ns==m[mid])
{
flag=1;
break;
}
}
if(flag==1)
{
System.out.println("Number found");
}
else
{
System.out.println("no not found");
}

}
}

import java.util.*;
class binary
{
public static void main (String arg[])
{
Scanner sc=new Scanner(System.in);
int m[]=new int[10];
int min, t, i, lb=0, ub=9, ns, mid, flag=0;
for( i=0;i<10;i++)
{
System.out.println("Enter number in position :"+i);
m[i]=sc.nextInt() ;

}
for(i=0;i<9;i++)
{
min=i;
for(int j=i+1;j<10;j++)

if(m[j]<m[min])

min=j;
t=m[i];
m[i]=m[min];
m[min]=t;
}
System. out. println ("Array in ascending order");
for(i=0;i<10;i++)
{
System.out.print(m[i]+" ");
}
System.out.println("Enter the number to be searched");
ns=sc.nextInt();
while(lb<=ub)
{
mid=(lb+ub) /2;
if(ns>m[mid])
{
lb=mid+1;
}
if(ns<m[mid])
{
ub=mid-1;
}
if(ns==m[mid])
{
flag=1;
break;
}
}
if(flag==1)
{
System.out.println("number found");
}
else
{
System.out.println("number not found");
}
}
}
For selection + binary

import java.util.*;
class binary {
public static void main (String arg[]) {
Scanner sc = new Scanner(System.in);
int m[] = new int[10];
int j, t = 0, i, lb = 0, ub = 9, ns, mid, flag = 0;

for(i = 0; i < 10; i++) {


System.out.println("Enter number in position :" + i);
m[i] = sc.nextInt();
}

// Bubble Sort with correct braces


for(i = 0; i < 10; i++) {
for(j = 0; j < 9 - i; j++) {
if(m[j] > m[j + 1]) {
t = m[j];
m[j] = m[j + 1];
m[j + 1] = t;
}
}
}

System.out.println("Array in ascending order:");


for(i = 0; i < 10; i++) {
System.out.print(m[i] + " ");
}

System.out.println("\nEnter the number to be searched:");


ns = sc.nextInt();

// Binary Search
while(lb <= ub) {
mid = (lb + ub) / 2;
if(ns > m[mid]) {
lb = mid + 1;
} else if(ns < m[mid]) {
ub = mid - 1;
} else {
flag = 1;
break;
}
}

if(flag == 1) {
System.out.println("Number found");
} else {
System.out.println("Number not found");
}
}
}

import java. util. *;


class convert
{
public static void main(String arg[])
{
Scanner Sc=new Scanner(System.in);
int i,p;
System.out.println("Enter a sentence");
String st=Sc.nextLine();
String st1=" ";
char ch, ch1;
st=" "+st;
p=st.length();
for(i=0;i<p;i++)
{
ch=st.charAt(i);
if(ch==' ')
{
ch1=st.charAt(i+1) ;
st1=st1+' '+Character. toUpperCase(ch1);
i=i+1;
}
else
{
st1=st1+ch;
}
}
System.out.println("String after change:"+ st1. trim());
}
}

import java.util.*;
class concat
{
public static void main (String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string in 1");
String st1=sc.nextLine();
System.out.println("Enter the string in 2");
String st2=sc.nextLine();
String st3=" ";
int i;
int p=st1.length() ;
int s=st2.length() ;
char ch1=' ', ch2=' ';
int max=Math.max(p, s);
for(i=0;i<max;i++)
{

if(i<p)
{
st3+=st1.charAt(i);
}
if(i<s)
{
st3+=st2.charAt(i);
}

}
System. out. println("The new word"+ " "+ st3);

}
}
import java.util.*;
class wonder {
public static void main (String arg[]) {
Scanner sc = new Scanner(System.in);

String won[] = new String[]{ "Chichen Itza", "Christ The Redeemer", "Taj Mahal", "Great
Wall of China", "Machu Pichu", "Petra", "Colosseum" };
String loc[] = new String[]{ "Mexico", "Brazil", "India", "China", "Peru", "Jordan", "Italy" };

System.out.println("Enter the location whose wonder you want to know:");


String con = sc.nextLine();

int k = -1;
String a = "";
int i;

for(i = 0; i < 7; i++) {


if(con.equalsIgnoreCase(loc[i])) {
k = 1;
a = won[i];
break;
}
}

if(k == 1) {
System.out.println(loc[i] + " - " + a);
} else {
System.out.println("Place does not contain any wonders");
}
}
}

You might also like