Học Java

[Thực hành] Minh hoạ thuật toán sắp xếp nổi bọt

NỘI DUNG BÀI VIẾT

Mục đích

Luyện tập cài đặt thuật toán sắp xếp nổi bọt.

Mô tả

Viết một chương trình cho phép quan sát các bước thực hiện của thuật toán sắp xếp nổi bọt.

Chương trình cho phép định nghĩa một mảng các số nguyên, hiển thị lần lượt các bước của thuật toán sắp xếp nổi bọt.

Hướng dẫn nộp bài:

  • Up mã nguồn lên github
  • Paste link github vào phần nộp bài

Hướng dẫn

Bước 1: Tạo lớp BubbleSortByStep, cài đặt hàm main cho phép nhập và hiển thị một mảng số nguyên

public class BubbleSortByStep {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         System.out.print("Enter list size:");         int size = scanner.nextInt();         int[] list = new int[size];         System.out.println("Enter " + list.length + " values:");         for (int i = 0; i < list.length; i++) {             list[i] = scanner.nextInt();         }         System.out.print("Your input list: ");         for (int i = 0; i < list.length; i++) {             System.out.print(list[i] + "\t");         }     }
    //codes below here
}

Bước 2: Cài đặt phương thức BubbleSortByStep (int[] list) để thực hiện và hiển thị các bước của thuật toán sắp xếp nổi bọt

 public static void bubbleSortByStep(int[] list) {     boolean needNextPass = true;     for (int k = 1; k < list.length && needNextPass; k++) {         needNextPass = false;         for (int i = 0; i < list.length - k; i++) {             if (list[i] > list[i + 1]) {                 /* Swap list[i] with list[i + 1] */                 System.out.println("Swap " + list[i] + " with " + list[i + 1]);                 int temp = list[i];                 list[i] = list[i + 1];                 list[i + 1] = temp;                  needNextPass = true; /* Next pass still needed */             }         }         /* Array may be sorted and next pass not needed */         if (needNextPass == false) {             System.out.println("Array may be sorted and next pass not needed");             break;         }         /* Show the list after sort */         System.out.print("List after the  " + k + "' sort: ");         for (int j = 0; j < list.length; j++) {             System.out.print(list[j] + "\t");         }         System.out.println();     } }

Bước 3: Bổ sung câu lệnh gọi phương thức bubbleSortByStep (list)  vào hàm main

System.out.println("\nBegin sort processing...");bubbleSortByStep(list);

Bước 4: Chạy chương trình. Quan sát kết quả trả về.

Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai.

Tài liệu + Khóa học lập trình FREE
Tài liệu + Khóa học lập trình FREE

DMCA.com Protection Status