36
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
Console.WriteLine("Thuật toán sắp xếp nổi bọt (Bubble Sort)");
8
int n, temp;
9
int [] a;
10
Console.WriteLine("Nhập vào số phần tử mảng:");
11
n = int.Parse(Console.ReadLine());
12
//Khởi tạo mảng
13
a = new int[n];
14
//Nhập dữ liệu cho từng phần tử mảng, chỉ số mảng bắt đầu từ 0
15
for(int i=0; i<n; i++)
16
{
17
Console.Write("a[{0}]:",i);
18
a[i] = int.Parse(Console.ReadLine());
19
}
20
//Thuật toán sắp xếp
21
for(int i=0; i<n-1; i++)
22
for(int j=i+1; j<n; j++)
23
if(a[i] > a[j])
24
{
25
temp = a[i];
26
a[i] = a[j];
27
a[j] = temp;
28
}
29
//In kết quả sắp xếp
30
Console.Write("Mảng sau khi sắp xếp:");
31
for(int i=0; i<n; i++)
32
{
33
Console.Write("{0} ,",a[i]);
34
}
35
}
36
}
Cached Result