हिंदी

Define a class to accept 10 characters from a user. Using bubble sort technique arrange them in ascending order. Display the sorted array and original array.

Advertisements

प्रश्न

Define a class to accept 10 characters from a user. Using bubble sort technique arrange them in ascending order. Display the sorted array and original array.

संक्षेप में उत्तर
Advertisements

उत्तर

import java.util.*; 
public class Charsort {
   public static void bubbleSort(char[] array) {
     int num = array.length;
     char temp = 0;
     for (int i = 0; i < num; i++) {
     for (int j = 1; j < (num - i);j++) {
         if (array[j - 1] > array[j]) {
         temp = array[j - 1];
         array[j - 1] = array[j]; 
        array[j] = temp;
       }
     }
   }
}
public static void main(String[] args) {
      char array[] = new char[10];
      Scanner sc=new Scanner(System.in);
      System.out.println(;'Enter 10 characters:");
      for(int i=0;i<10;i++)
          array[i]=sc.nextLine().charAt(0);
      System.out.println("Array Before Bubble Sort:
      " + Arrays.toString(array));
      bubbleSort(array);
      System.out.println("Array After Bubble Sort:"
      + Arrays.toString(array));
   }
}
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2022-2023 (March) Official
Advertisements
Share
Notifications



      Forgot password?
Use app×