Bubble sort code for sorting numbers in ascending order
import java.util.Scanner;
public class Bubble_Asc{
public static void sort_number_asc(){
// input array elements
Scanner sc = new Scanner(System.in);
int a[] = new int[5];
System.out.println("Enter five array elements");
for(int i=0; i<a.length; i++){
a[i] = sc.nextInt();
}
// sort array elements
for(int i=0; i<a.length; i++){
for(int j=0; j<a.length-1-i; j++){
if( a[j]>a[j+1] ){
//swap elements
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
// output the sorted array
System.out.println("Sorted array is");
for(int i=0; i<a.length; i++){
System.out.print(a[i] + " ");
}
}
}
public class Bubble_Asc{
public static void sort_number_asc(){
// input array elements
Scanner sc = new Scanner(System.in);
int a[] = new int[5];
System.out.println("Enter five array elements");
for(int i=0; i<a.length; i++){
a[i] = sc.nextInt();
}
// sort array elements
for(int i=0; i<a.length; i++){
for(int j=0; j<a.length-1-i; j++){
if( a[j]>a[j+1] ){
//swap elements
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
// output the sorted array
System.out.println("Sorted array is");
for(int i=0; i<a.length; i++){
System.out.print(a[i] + " ");
}
}
}
Bubble sort code for sorting numbers in descending order
import java.util.Scanner;
public class Bubble_Desc{
public static void sort_number_desc(){
// input array elements
Scanner sc = new Scanner(System.in);
int a[] = new int[5];
System.out.println("Enter five array elements");
for(int i=0; i<a.length; i++){
a[i] = sc.nextInt();
}
// sort array elements
for(int i=0; i<a.length; i++){
for(int j=0; j<a.length-1-i; j++){
if( a[j]<a[j+1] ){
//swap elements
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
// output the sorted array
System.out.println("Sorted array is");
for(int i=0; i<a.length; i++){
System.out.print(a[i] + " ");
}
}
}
Bubble sort code for sorting strings in ascending order
import java.util.Scanner;
public class Bubble_Asc_Strings{
public static void sort_string_asc(){
// input array elements
Scanner sc = new Scanner(System.in);
String a[] = new String[5];
System.out.println("Enter five array elements");
for(int i=0; i<a.length; i++){
a[i] = sc.next();
}
// sort array elements
for(int i=0; i<a.length; i++){
for(int j=0; j<a.length-1-i; j++){
if( a[j].compareTo(a[j+1])>0 ){
//swap elements
String temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
// output the sorted array
System.out.println("Sorted array is");
for(int i=0; i<a.length; i++){
System.out.print(a[i] + " ");
}
}
}
Bubble sort code for sorting strings in descending order
import java.util.Scanner;
public class Bubble_Desc_Strings{
public static void sort_string_desc(){
// input array elements
Scanner sc = new Scanner(System.in);
String a[] = new String[5];
System.out.println("Enter five array elements");
for(int i=0; i<a.length; i++){
a[i] = sc.next();
}
// sort array elements
for(int i=0; i<a.length; i++){
for(int j=0; j<a.length-1-i; j++){
if( a[j].compareTo(a[j+1])<0 ){
//swap elements
String temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
// output the sorted array
System.out.println("Sorted array is");
for(int i=0; i<a.length; i++){
System.out.print(a[i] + " ");
}
}
}
Youtube video link explaining the above code is
No comments:
Post a Comment