સિલેક્શન સૉર્ટ હજી ન ગોઠવાયેલા ભાગમાં સૌથી નાનું મૂલ્ય શોધીને આગળની જગ્યાએ મૂકે છે. દરેક ચક્કર પછી વધુ એક મૂલ્ય તેની આખરી જગ્યાએ પહોંચે છે. અહીં જગ્યાઓની દરેક જોડીની એક વખત સરખામણી થાય છે. યાદી પહેલેથી ગોઠવાયેલી હોય તો પણ કુલ 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