सिलेक्शन सॉर्ट अजून न लावलेल्या भागातलं सर्वांत लहान मूल्य शोधून पुढच्या जागी ठेवतो. प्रत्येक फेरीनंतर आणखी एक मूल्य त्याच्या शेवटच्या जागी पोहोचतं. इथे जागांच्या प्रत्येक जोडीची एकदा तुलना होते. यादी आधीच क्रमाने असेल तरी एकूण n(n − 1)/2 तुलना होतात.
एक सोपे उदाहरण
पहिल्या जागेसाठी 1 निवडा. मग दुसऱ्या जागेसाठी 2 निवडा. क्रमाने लागलेल्या संख्या स्क्रीनवर दाखवा.
सुरुवातीला3 1 2
निकाल1 2 3
lesson.cpp
#include <iostream>
int main() {
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) {
std::cout << values[i] << '\n';
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²)
C++ मध्ये सिलेक्शन सॉर्ट
C++ सोर्स कोड
#include <iostream>
using namespace std;
class SelectionSortExample
{
// Access specifier
public:
void selectionSort(int arr[], size_t size) {
for(size_t i = 0; i < size - 1; i++) {
int minIndex = i;
for(size_t j = i + 1; j < size; j++) {
if(arr[minIndex] > arr[j]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
void printArray(const int arr[], size_t size) {
for(size_t i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main(int argc, const char * argv[])
{
// Declare an object of class geeks
SelectionSortExample obj;
int arr[] = { 15, 3, 12, 6, -9, 9, 0 };
const size_t SIZE = sizeof(arr) / sizeof(*arr);
cout << "Before Sorting: ";
obj.printArray(arr, SIZE);
obj.selectionSort(arr, SIZE);
cout << "After Sorting: ";
obj.printArray(arr, SIZE);
return 0;
}
आउटपुट
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 ची जागा अदलाबदल करा. पहिल्या जागेवर आता योग्य मूल्य बसलं.
पुढच्या जागेपासून हेच पुन्हा करा. एकच मूल्य उरेल तेव्हा संपूर्ण अॅरे क्रमात लागलेला असेल.
C++ मध्ये सिलेक्शन सॉर्ट टप्प्याटप्प्याने समजून घ्या
C++ सोर्स कोड
#include <iostream>
using namespace std;
class SelectionSortExample
{
// Access specifier
public:
void selectionSort(int arr[], size_t size) {
// Loop all the element in an array except the last element
for(size_t i = 0; i < size - 1; i++) {
int minIndex = i;
// Find an index of a lowest element in the right-hand side
// corresponding to the element arr[i].
for(size_t j = i + 1; j < size; 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;
}
}
void printArray(const int arr[], size_t size) {
// Loop the item and print each item in the console with white space seperated
for(size_t i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};
int main(int argc, const char * argv[])
{
// Declare an object of class geeks
SelectionSortExample obj;
int arr[] = { 15, 3, 12, 6, -9, 9, 0 };
const size_t SIZE = sizeof(arr) / sizeof(*arr);
cout << "Before Sorting: ";
obj.printArray(arr, SIZE);
obj.selectionSort(arr, SIZE);
cout << "After Sorting: ";
obj.printArray(arr, SIZE);
return 0;
}
आउटपुट
Before Sorting: 15 3 12 6 -9 9 0
After Sorting: -9 0 3 6 9 12 15