Selection sort

Ang selection sort ay maituturing na pinakasimple sa lahat ng mga sorting algorithms. Isinasagawa ito sa pamamagitan ng paghahati sa mga numerong isasaayos sa dalawang bahagi. Ang isang bahagi ay binubuo ng mga numerong naayos na samantalang ang natirang bahagi ay binubuo naman ng mga numerong wala pa sa ayos.

Nagsisimula ang prosesong ito na walang lamang ang unang bahagi (naayos) habang ang ikalawang bahagi naman (hindi pa naayos) ay kinabibilangan ng lahat ng numerong nasa listahan.

Isinasagawa ang selection sort sa pamamagitan ng paulit-ulit na paghanap ng pinakamaliit (o pinakamalaking) numero sa hindi pa naayos na bahagi ng listahan at paglagay ng nasabing numero sa dulo ng naayos ng bahagi. Matatapos ang proseso kapag ang lahat ng laman ng hindi pa naayos na bahagi ay nailipat na sa naayos ng bahagi.

Ang selection sort ay pinakamabuting gamitin sa mga maliliit na listahan. Pinakamalala naman itong gamiting kapag ang listahan ay nakaayos sa baligtad na paraan.

Ang complexity ng algorithm na ito ay O(n2). Gayunpaman, sa halos lahat ng pagkakataon ay mas mabilis ito sa bubble sort at gnome sort na may parehong complexity. May mga pagkakataon din kung saan mas mabuti itong kagimitin kaysa sa mga mas kumplikadong mga algorithm.

Halimbawa:

5 4 2 3 1
1 4 2 3 5
1 2 4 3 5
1 2 3 4 5
1 2 3 4 5

Code snippets:

Java

public void selectionSort(int[] arr) {

     int i, j, minIndex, tmp;
     int n = arr.length;
     for (i = 0; i < n - 1; i++) {
           minIndex = i;
           for (j = i + 1; j < n; j++)
                 if (arr[j] < arr[minIndex])
                       minIndex = j;
           if (minIndex != i) {
                 tmp = arr[i];
                 arr[i] = arr[minIndex];
                 arr[minIndex] = tmp;
           }
     }

}

C++

void selectionSort(int arr[], int n) {

     int i, j, minIndex, tmp;    
     for (i = 0; i < n - 1; i++) {
           minIndex = i;
           for (j = i + 1; j < n; j++)
                 if (arr[j] < arr[minIndex])
                       minIndex = j;
           if (minIndex != i) {
                 tmp = arr[i];
                 arr[i] = arr[minIndex];
                 arr[minIndex] = tmp;
           }
     }

}

Mga Sanggunian

http://www.algolist.net/Algorithms/Sorting/Selection_sort Naka-arkibo 2012-12-09 sa Wayback Machine.
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/selectionSort.htm Naka-arkibo 2011-03-20 sa Wayback Machine.
http://www.cprogramming.com/tutorial/computersciencetheory/sorting2.html

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.