0% found this document useful (0 votes)
3 views2 pages

sort

The document contains Java code snippets demonstrating various functionalities such as sorting lists in ascending and descending order, converting between arrays and lists, and checking for perfect numbers and prime numbers. It includes examples of using Bubble Sort, as well as string manipulation techniques. Additionally, it features user input handling to check if a number is perfect.

Uploaded by

hieudnhe181465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

sort

The document contains Java code snippets demonstrating various functionalities such as sorting lists in ascending and descending order, converting between arrays and lists, and checking for perfect numbers and prime numbers. It includes examples of using Bubble Sort, as well as string manipulation techniques. Additionally, it features user input handling to check if a number is perfect.

Uploaded by

hieudnhe181465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

List<Integer> list = Arrays.

asList(5, 2, 9, 1, 6);

// Tăng dần (mặc định)


list.sort(Comparator.naturalOrder());
System.out.println("Tăng dần: " + list); // [1, 2, 5, 6, 9]

// Giảm dần
list.sort(Comparator.reverseOrder());
System.out.println("Giảm dần: " + list); // [9, 6, 5, 2, 1]

Collections.sort(list); // tăng dần


Collections.sort(list, Comparator.reverseOrder()); // giảm dần
///////////////////////////////////////////////////////////////////////////////////
////////
public class BubbleSortDemo {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 1, 6};

for (int i = 0; i < arr.length - 1; i++) {


for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Đổi chỗ 2 phần tử
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

// In ra mảng đã sắp xếp


System.out.print("Mảng sau khi sắp xếp: ");
for (int x : arr) {
System.out.print(x + " ");
}
}
}
///////////////////////////////////////////////////////////////////////////////////
//////
Integer[] arr = {1, 2, 3};
List<Integer> list = new ArrayList<>(Arrays.asList(arr));

// List → Array
Integer[] backToArray = list.toArray(new Integer[0]);

// int[] → List<Integer>
int[] intArr = {4, 5, 6};
List<Integer> list2 = Arrays.stream(intArr).boxed().toList();

// List<Integer> → int[]
int[] intArr2 = list2.stream().mapToInt(i -> i).toArray();

///////////////////////////////////////////////////////////////////////////////////
////
String s = "Minh,cute,pro,max";
List<String> list = Arrays.asList(s.split(","));
System.out.println(list); // [Minh, cute, pro, max]
List<String> list = Arrays.asList("Minh", "cute", "pro", "max");
String s = String.join("-", list);
System.out.println(s); // Minh-cute-pro-max

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n;
boolean res;
do {
System.out.print("Input n = ");
n = sc.nextInt();
} while (n <= 0);
res = checkPerfectNumber(n);
if (res) {
System.out.println(n + " la so hoan hao");
} else {
System.out.println(n + " khong phai la so hoan hao");
}
}
public static boolean checkPerfectNumber(int n) {
int sum = 0;
for (int i = 1; i <= n/2; i++) {
if (n % i == 0) {
sum += i;
}
}
return sum == n;
}

public static boolean isPrime(int n) {


if (n < 2) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}

// Hàm kiểm tra số hoàn hảo


public static boolean isPerfect(int n) {
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
}
}
return sum == n;
}

You might also like