peri hoki perihoki clarustologistics rimashaop.com fenomena gates of olympus buka peluang besar bagi duta76 cara dapat pola emas rahasia di duta7 wild west gold trik menang pgsoft mahjong ways 2 modal receh maxwin duta76 duta76 main pgsoft mahjong ways 2 di sela istirahat scatter hitam mahjong wins 3 spin pasti jepe duta76 pola petir tersembunyi gates of olympus perihoki pgsoft perkuat mahjong ways 2 di akun perihoki perihoki beri pola jitu mahjong wins 3 auto maxwin exclusive perihoki gates of olympus hadirkan fitur super scatter pgsoft mahjong ways perihoki punya irama scatter sendiri pola jitu scatter hitam mahjong wins 3 auto maxwin di perihoki akun baru langsung panen di gates of olympus di duta76 cara buat akun duta76 menjadi gacor main mahjong ways 2 pgsoft mahjong wins 3 duta76 viral karena tembus maxwin dalam 7 menit cuan 10rb scatter mahjong ways santai main mahjong wins gacor scatter emas 257jt mahjong wins pegawai menang mahjong awsbet teknik menang mahjong ways yang efektif
A PHP Error was encountered
Severity: Warning
Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time
Filename: Session/Session.php
Line Number: 282
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent
Filename: Session/Session.php
Line Number: 294
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time
Filename: Session/Session.php
Line Number: 304
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time
Filename: Session/Session.php
Line Number: 314
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time
Filename: Session/Session.php
Line Number: 315
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time
Filename: Session/Session.php
Line Number: 316
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time
Filename: Session/Session.php
Line Number: 317
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time
Filename: Session/Session.php
Line Number: 375
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time
Filename: drivers/Session_files_driver.php
Line Number: 108
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: session_set_save_handler(): Cannot change save handler when headers already sent
Filename: Session/Session.php
Line Number: 110
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: session_start(): Cannot start session when headers already sent
Filename: Session/Session.php
Line Number: 143
Backtrace:
File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct
File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once
File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once
Java - Sắp xếp mảng tăng dần sử dụng phương pháp nổi bọt (Bubble sort).
TỔNG QUAN
CẤU TRÚC ĐIỀU KHIỂN
VÒNG LẶP
CHUỖI VÀ MẢNG
COLLECTIONS
THƯ VIỆN QUAN TRỌNG
HƯỚNG ĐỐI TƯỢNG
XỬ LÝ LUỒNG
EXCEPTION
LÀM VIỆC VỚI FILE
THAM KHẢO
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
Java - Sắp xếp mảng tăng dần sử dụng phương pháp nổi bọt (Bubble sort).
Viết chương trình sắp xếp tăng dần các phần tử trong mảng bằng phương pháp nổi bọt (Bubble sort ).
Bài giải
-------------------- ######## --------------------
Trong phần bài học, tôi đã hướng dẫn các bạn phương pháp phương pháp sắp xếp thông qua phép đổi chỗ trực tiếp (Exchange sort ). Ngoài ra, chúng ta còn một phương pháp sắp xếp cũng khá phổ biến mà tôi sẽ sử dụng trong bài tập này đó là phương pháp nổi bọt (Bubble sort ). Ý tưởng chính của phương pháp nổi bọt là các phần tử trong mảng sẽ được duyệt từ đáy lên đỉnh. Trên đường duyệt, nếu phần tử đứng sau nhỏ hơn phần tử đứng trước thì đổi chỗ chúng cho nhau. Do đó, trong lần duyệt đầu tiên, phần tử có giá trị nhỏ nhất sẽ được chuyển lên đỉnh. Lần duyệt thứ 2, khóa có giá trị nhỏ thứ 2 được chuyển lên vị trí thứ 2... Và cứ tiếp tục như thế cho đến khi toàn bộ mảng các phần tử đã được sắp xếp theo thứ tự tăng dần.
Bài viết này được đăng tại freetuts.net , không được copy dưới mọi hình thức.
Bài giải
public static void main(String[] args) {
int n, tempSort; // số phần tử của mảng
Scanner scanner = new Scanner(System.in);
// nếu n < 0 thì phải nhập lại
do {
System.out.println("Nhập vào số phần tử của mảng: ");
n = scanner.nextInt();
} while (n < 0);
// khai báo và cấp phát bộ nhớ cho mảng
int array[] = new int[n];
// nhập giá trị các phần tử cho mảng
for (int i = 0; i < n; i++) {
System.out.print("Nhập phần tử thứ " + i + ": ");
array[i] = scanner.nextInt();
}
// hiển thị mảng ban đầu
System.out.println("\nMảng ban đầu: ");
for (int i = 0; i < n; i++) {
System.out.print(array[i] + "\t");
}
// sắp xếp mảng sử dụng phương pháp Bubble sort
// duyệt i từ 0 đến n - 2
// sau mỗi lần duyệt j = n - 1 đến j >= 1
// thì sẽ tìm được phần tử nhỏ nhất
// sau đó tăng i lên 1 và quay lại duyệt j
// khi i > n - 1 thì ngừng vòng lặp
for (int i = 0; i < n - 1; i++) {
for (int j = n - 1; j >= 1; j--) {
// nếu phần tử đứng sau nhỏ hơn phần tử đứng trước
// thì đổi chỗ 2 phần tử đó cho nhau
// với cách sắp xếp này thì trong lần duyệt đầu tiên
// phần tử nhỏ nhất sẽ được chuyển lên trên cùng.
if (array[j] < array[j - 1]) {
tempSort = array[j];
array[j] = array[j-1];
array[j-1] = tempSort;
}
}
}
System.out.println("\nMảng sau khi sắp xếp: ");
for (int i = 0; i < n; i++) {
System.out.print(array[i] + "\t");
}
}
Kết quả sau khi biên dịch chương trình:
Câu hỏi thường gặp liên quan:
Cùng chuyên mục: