സെലക്ഷൻ സോർട്ട് ഇനിയും ക്രമത്തിലാക്കാത്ത ഭാഗത്തെ ഏറ്റവും ചെറിയ മൂല്യം കണ്ടെത്തി അടുത്ത സ്ഥാനത്ത് വയ്ക്കുന്നു. ഓരോ ചുറ്റിലും ഒരു മൂല്യം കൂടി അതിന്റെ അവസാന സ്ഥാനത്തെത്തും. ഇവിടെ ഓരോ ജോഡി സ്ഥാനങ്ങളും ഒരുതവണ വീതം താരതമ്യം ചെയ്യും. പട്ടിക നേരത്തേ ക്രമത്തിലായാലും ആകെ n(n − 1)/2 താരതമ്യങ്ങൾ വേണം.
ഒരു ലളിതമായ ഉദാഹരണം
ആദ്യ സ്ഥാനത്തേക്ക് 1 തിരഞ്ഞെടുക്കുക. പിന്നെ രണ്ടാം സ്ഥാനത്തേക്ക് 2 തിരഞ്ഞെടുക്കുക. ക്രമത്തിലായ സംഖ്യകൾ സ്ക്രീനിൽ കാണിക്കുക.
തുടക്കത്തിൽ3 1 2
ഫലം1 2 3
Main.java
public class Main {
public static void main(String[] args) {
int[] values = {3, 1, 2};
int i = 0;
while (i < 2) {
int smallest = i;
int j = i + 1;
while (j < 3) {
if (values[j] < values[smallest]) {
smallest = j;
}
j += 1;
}
int saved = values[i];
values[i] = values[smallest];
values[smallest] = saved;
i += 1;
}
i = 0;
while (i < 3) {
System.out.println(values[i]);
i += 1;
}
}
}
ഔട്ട്പുട്ട്
1
2
3
നിത്യജീവിതത്തിലെ ഉദാഹരണങ്ങൾ
ഭാരം കുറഞ്ഞ പാഴ്സലുകൾ ആദ്യം കയറ്റാം
പാഴ്സലുകളുടെ ഭാരം 8, 3, 5 കിലോഗ്രാം ആണ്. ഇതിൽ ഏതാണ് ആദ്യത്തെ ലോഡിംഗ് സ്ഥാനത്ത് വരിക?
തുടക്കത്തിൽ8 3 5
ഫലം3 8 5
മൂന്നും സ്കാൻ ചെയ്യുക, തുടർന്ന് മുന്നിലുള്ള പാഴ്സലുമായി 3 കൈമാറുക.
ഏറ്റവും ആദ്യത്തെ തീയതി തിരഞ്ഞെടുക്കാം
തരംതിരിക്കാത്ത ഫോമുകൾ 9, 12, 4 എന്നീ ദിവസങ്ങൾ കാണിക്കുന്നു. ഏത് തീയതിയാണ് ആദ്യ ട്രേയിൽ പോകുന്നത്?
തുടക്കത്തിൽ9 12 4
ഫലം4
ആദ്യത്തെ രണ്ട് തീയതികൾ മാത്രം വായിക്കുന്നത് ദിവസം 4 നഷ്ടമാകും.
പുസ്തകങ്ങൾ അതേ ഷെൽഫിൽ ക്രമപ്പെടുത്താം
1, 2 ബുക്കുകൾ ഇതിനകം ഫിക്സ് ചെയ്തിട്ടുണ്ട്. 5, 3 ബുക്കുകൾ മാത്രമാണ് ബാക്കി. ഏത് സെക്ഷനാണ് സ്കാൻ ചെയ്യേണ്ടത്?
തുടക്കത്തിൽ1 2 • 5 3
ഫലം1 2 3 5
നിശ്ചിത പ്രിഫിക്സ് അതേപടി നിലനിർത്തുക. ബാക്കിയുള്ള ജോഡിയിൽ നിന്ന് 3 തിരഞ്ഞെടുത്ത് അത് സ്ഥാനത്തേക്ക് മാറ്റുക.
വേണ്ടിവരുന്ന ജോലിയും പരിമിതികളും
ഇനങ്ങൾ നേരത്തേ ക്രമത്തിലാണെങ്കിലും സെലക്ഷൻ സോർട്ട് ബാക്കിയുള്ളവയെല്ലാം പരിശോധിക്കും. ഇതിന് O(n²) താരതമ്യങ്ങൾ വേണം. സ്ഥാനങ്ങൾ പരസ്പരം മാറ്റുന്നത് പരമാവധി n−1 തവണയാണ്.
സമയ സങ്കീർണ്ണത
ഏറ്റവും കുറച്ചു സമയം വേണ്ട സാഹചര്യം: Θ(n²)
ശരാശരി സാഹചര്യം: Θ(n²)
ഏറ്റവും കൂടുതൽ സമയം വേണ്ട സാഹചര്യം: Θ(n²)
Java ഭാഷയിൽ സെലക്ഷൻ സോർട്ട്
Java സോഴ്സ് കോഡ്
class SelectionSortExample {
public void selectionSort(int[] arr) {
int length = arr.length;
for(int i = 0; i < length - 1; i++) {
int minIndex = i;
for(int j = i + 1; j < length; j++) {
if(arr[minIndex] > arr[j]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public void printArray(int[] arr) {
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
SelectionSortExample obj = new SelectionSortExample();
int[] arr = { 15, 3, 12, 6, -9, 9, 0 };
System.out.print("Before Sorting: ");
obj.printArray(arr);
obj.selectionSort(arr);
System.out.print("After Sorting: ");
obj.printArray(arr);
}
}
ഔട്ട്പുട്ട്
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15
പരസ്യങ്ങൾ
ഉദാഹരണം ഓരോ ഘട്ടമായി നോക്കാം
[15, 3, 12, 6, -9, 9, 0] ഉപയോഗിച്ച് തുടങ്ങുക.
ഇൻഡക്സ് 0-ൽ തുടങ്ങൂ. ബാക്കി വിലകൾ ഓരോന്നായി നോക്കി ഏറ്റവും ചെറിയ വിലയായ −9 ഏത് സ്ഥാനത്താണെന്ന് ഓർത്തുവയ്ക്കൂ.
ആദ്യത്തെ വില 15 ആണ്. അതിന്റെ സ്ഥാനവും −9-ന്റെ സ്ഥാനവും തമ്മിൽ മാറ്റൂ. ആദ്യ സ്ഥാനത്ത് ഇനി ശരിയായ വിലയാണ്.
അടുത്ത സ്ഥാനം മുതൽ ഇതുതന്നെ ആവർത്തിക്കുക. ഒരു മൂല്യം മാത്രം ബാക്കിയാകുമ്പോൾ അറേ മുഴുവനും ക്രമത്തിലായിരിക്കും.
Java ഭാഷയിൽ സെലക്ഷൻ സോർട്ട് ഘട്ടം ഘട്ടമായി
Java സോഴ്സ് കോഡ്
class SelectionSortExample {
public void selectionSort(int[] arr) {
// Get the length of an array
int length = arr.length;
// Loop all the element in an array except the last element
for(int i = 0; i < length - 1; i++) {
int minIndex = i;
// Find an index of a lowest element in the right-hand side
// corresponding to the element arr[i].
for(int j = i + 1; j < length; j++) {
if(arr[minIndex] > arr[j]) {
minIndex = j;
}
}
// Swap the lowest element with the current element (i.e. arr[i] <=> arr[minIndex])
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public void printArray(int[] arr) {
// Loop the item and print each item in the console with white space seperated
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
SelectionSortExample obj = new SelectionSortExample();
int[] arr = { 15, 3, 12, 6, -9, 9, 0 };
System.out.print("Before Sorting: ");
obj.printArray(arr);
obj.selectionSort(arr);
System.out.print("After Sorting: ");
obj.printArray(arr);
}
}
ഔട്ട്പുട്ട്
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15