Programs On Bubble Sort & Selection Sort
Programs On Bubble Sort & Selection Sort
import java.util.Scanner;
public class BubbleSortNumbers
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
int numbers[]=new int[10];
for(int i=0; i<10; i++)
{
System.out.println("Enter the number "+(i+1));
numbers[i]=sc.nextInt();
}
System.out.println("\nUnsorted Array ");
for(int i=0; i<10; i++)
{
System.out.println(numbers[i]);
}
//Bubble Sort technique
for(int i=0; i<10; i++)
{
for(int j=0; j<10-1-i; j++)
{
if(numbers[j]>numbers[j+1])
{
int temp=numbers[j];
numbers [j]= numbers[j+1];
numbers[j+1]=temp;
}
}
}
System.out.println("\n\nSorted Array");
for(int i=0; i<10; i++)
{
System.out.println(numbers[i]);
}
}
}
Sorting characters in ascending order using Bubble Sort Technique
import java.util.Scanner;
public class BubbleSortNumbers
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
char ch[]=new char[10];
for(int i=0; i<10; i++)
{
System.out.println("Enter the number "+(i+1));
ch[i]=sc.next().charAt(0);
}
System.out.println("\nUnsorted Array ");
for(int i=0; i<10; i++)
{
System.out.println(ch[i]);
}
//Bubble Sort technique
for(int i=0; i<10; i++)
{
for(int j=0; j<10-1-i; j++)
{
if(ch[j]>ch[j+1])
{
char temp=ch[j];
ch [j]= ch [j+1];
ch [j+1]=temp;
}
}
}
System.out.println("\n\nSorted Array");
for(int i=0; i<10; i++)
{
System.out.println(ch [i]);
}
}
}
Sorting Strings in ascending order using Bubble Sort Technique
import java.util.Scanner;
public class BubbleSortNumbers
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
String words[]=new String[10];
for(int i=0; i<10; i++)
{
System.out.println("Enter the number "+(i+1));
words[i]=sc.nextLine();
}
System.out.println("\nUnsorted Array ");
for(int i=0; i<10; i++)
{
System.out.println(words[i]);
}
//Bubble Sort technique
for(int i=0; i<10; i++)
{
for(int j=0; j<10-1-i; j++)
{
if(words[j].compareTo(words[j+1]>0)
{
String temp=words[j];
words [j]= words [j+1];
words [j+1]=temp;
}
}
}
System.out.println("\n\nSorted Array");
for(int i=0; i<10; i++)
{
System.out.println(words[i]);
}
}
}
Sorting numbers in ascending order using Selection Sort Technique
import java.util.Scanner;
public class SelectionSortNumbers
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
int numbers[]=new int[10];
for(int i=0; i<10; i++)
{
System.out.println("Enter the number "+(i+1));
numbers[i]=sc.nextInt();
}
System.out.println("\nUnsorted Array ");
for(int i=0; i<10; i++)
{
System.out.println(numbers[i]);
}
//Selection Sort technique
int i,j,pos;
int small,temp;
for(i=0; i<10; i++)
{
small=numbers[i];
pos=i;
for(j=i+1; j<10; j++)
{
if(numbers[j]<small)
{
small=numbers[j];
pos=j;
}
}
temp=numbers[i];
numbers [i]= numbers[pos];
numbers[pos]=temp;
}
System.out.println("\n\nSorted Array");
for(int k=0; k<10; k++)
{
System.out.println(numbers[k]);
}
}
}
Sorting characters in ascending order using Selection Sort Technique
import java.util.Scanner;
public class SelectionSortNames
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
char ch[]=new char[10];
for(int i=0; i<10; i++)
{
System.out.println("Enter the word "+(i+1));
ch[i]=sc.next().charAt(0);
}
System.out.println("\nUnsorted Array ");
for(int i=0; i<10; i++)
{
System.out.println(ch[i]);
}
//Selection Sort technique
int i,j,pos;
char small,temp;
for(i=0; i<10; i++)
{
small=ch[i];
pos=i;
for(j=i+1; j<10; j++)
{
if(ch[j]<small)
{
small=ch[j];
pos=j;
}
}
temp=ch[i];
ch[i]=names[pos];
ch [pos]=temp;
}
System.out.println("\n\nSorted Array");
for(int k=0; k<10; k++)
{
System.out.println(ch[k]);
}
}
}
Sorting String in ascending order using Selection Sort Technique
import java.util.Scanner;
public class SelectionSortNames
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
String names[]=new String[10];
for(int i=0; i<10; i++)
{
System.out.println("Enter the word "+(i+1));
names[i]=sc.nextLine();
}
System.out.println("\nUnsorted Array ");
for(int i=0; i<10; i++)
{
System.out.println(names[i]);
}
//Selection Sort technique
int i,j,pos;
String small,temp;
for(i=0; i<10; i++)
{
small=names[i];
pos=i;
for(j=i+1; j<10; j++)
{
if(names[j].compareTo(small)<0)
{
small=names[j];
pos=j;
}
}
temp=names[i];
names[i]=names[pos];
names[pos]=temp;
}
System.out.println("\n\nSorted Array");
for(int k=0; k<10; k++)
{
System.out.println(names[k]);
}
}
}