HACKIS - Hacking Internet Security
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Search
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
» Tuyệt Kỹ Đong Giai Chân Kinh (tuyệt Kỹ cua trai)
Selection Sort EmptyThu Aug 23, 2012 5:38 am by Admin

» Tuyệt kỹ cua giai
Selection Sort EmptyThu Aug 23, 2012 5:36 am by Admin

» NETCAT.........
Selection Sort EmptyMon Aug 13, 2012 6:35 am by Admin

» Bảo mật CSDL bằng phương pháp mã hóa.
Selection Sort EmptyTue Apr 17, 2012 10:04 pm by Admin

» Hàm mã hóa MD5 bằng JavaScript
Selection Sort EmptyTue Apr 17, 2012 10:03 pm by Admin

» Giá của món quà
Selection Sort EmptyFri Apr 13, 2012 6:01 am by Admin

» Sẽ chỉ yêu ai?
Selection Sort EmptyFri Apr 13, 2012 6:01 am by Admin

» Cách đọc bảng chữ cái!
Selection Sort EmptyThu Apr 12, 2012 10:37 pm by Admin

» Gắn trojan, keylog, virus vào website, forum
Selection Sort EmptyTue Apr 10, 2012 1:14 am by Admin

Affiliates
free forum


Selection Sort

Go down

Selection Sort Empty Selection Sort

Post  Admin Sun Jun 12, 2011 9:47 am

1. Chọn một phần từ lớn nhất trong n phần tử: a[0], a[1], ..., a[n-1], sau đó hoán vị với phần tử a[0].
2. Chọn phần tử lớn nhất trong n - 1 phần tử từ: a[1], a[2], ..., a[n-1], sau đó hoán vị với phẩn tử a[1].
3. Tổng quát: Ta thấy ở lần thứ i, ta sẽ tìm phần tử lớn nhất trong n - i phần tử từ: a[i], a[i+1],...,a[n-1], sau đó hoán vị no với a[i].
Ta có chương trình minh họa:

Code:
public class SelectionSort {

    // Sort by descending
    public static void sort(int[] array) {

        for (int i = 0; i < array.length; i++) {
            int max = array[i]; // Lưu phần tử lớn nhất
            int index = i; // lưu vị trí chứa phần tử lớn nhất
            for (int j = i + 1; j < array.length; j++) {
                if(max < array[j]){
                    max = array[j];
                    index = j;
                }
            }
            // Nếu chỉ số đã thay đổi, ta sẽ hoán vị
            if(index != i){
                int temp = array[i];
                array[i] = array[index];
                array[index] = temp;
            }
        }

    }
}
Admin
Admin
Admin

Tổng số bài gửi : 782
Join date : 2009-08-15

https://hackis.forumvi.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum