Selection Sort | Practice | GeeksforGeeks

Given an unsorted array of size N, use selection sort to sort arr[] in increasing order.

Example 1:
Input:
N = 5
arr[] = {4, 1, 3, 9, 7}
Output:
1 3 4 7 9
Explanation:
Maintain sorted (in bold) and unsorted subarrays.
Select 1. Array becomes 1 4 3 9 7.
Select 3. Array becomes 1 3 4 9 7.
Select 4. Array becomes 1 3 4 9 7.
Select 7. Array becomes 1 3 4 7 9.
Select 9. Array becomes 1 3 4 7 9.
Example 2:
Input:
N = 10
arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
Output:
1 2 3 4 5 6 7 8 9 10

Your Task:  
You dont need to read input or print anything. Complete the functions select() and selectionSort() ,where select() takes arr[] and starting point of unsorted array i as input parameters and returns the selected element for each iteration in selection sort, and selectionSort() takes the array and it's size and sorts the array.

Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 10^3
0
debasishtewary51 day ago
static  int  select(int arr[], int i){       // code here such that selectionSort() sorts arr[]       int in=0;int min=arr[i];for(int j=i+1;j<arr.length;j++) { if(arr[j]<min) {  min=arr[j]; in=j; } }return in;}void selectionSort(int arr[], int n){    //code here    for(int i=0;i<n;i++){             int j=Solution.select(arr,i);     if(j!=0){     int a=arr[j];     arr[j]=arr[i];     arr[i]=a;     }    }}
0
angelettamorgan14 days ago
Eeee
0
sangrambachu4 days ago
class Solution
{
  select(arr,i){
     // code here such that selectionSort() sorts arr[]
  }

  //Function to sort the array using selection sort algorithm.
  selectionSort(arr,n){
   //code here
   for(let i=0; i<n-1; i++) {
       let minIndex = i;
       
       for(let j=i+1; j<n; j++) {
           if(arr[j] < arr[minIndex]) {
               minIndex = j;
           }
       }
       
       if(minIndex != i) {
           let temp = arr[i];
           arr[i] = arr[minIndex];
           arr[minIndex] = temp;
       }
   }
  }
0
koulikmaity1 week ago
int select(int arr[], int i)   {       // code here such that selectionSort() sorts arr[]   }       void selectionSort(int arr[], int n)   {      for(int i=0; i<n-1; i++)      {          for(int j=i+1; j<n; j++)          {              if(arr[j] < arr[i] )              {                  swap(arr[i], arr[j]);              }          }      }   }
0
shashwatchaurasia833 weeks ago
int select(int arr[], int i){   // leave as it is }
void selectionSort(int arr[], int n){   for(int i=0;i<n-2;i++)    {         int Min_Idx=i;                  for(int j=i+1;j<n-1;j++)         {             if(arr[j]<arr[Min_Idx])                  Min_Idx=j;         }                  swap(&arr[Min_Idx],&arr[i]);     }}
0
mitradiptamoy4 weeks ago
void selectionSort(int arr[], int n)   {      //code here      for(int i=0;i<=n-2;i++)      {          int min_ind=i;          for(int j=i+1;j<=n-1;j++)          {              if(arr[j]<arr[min_ind])              {                  min_ind=j;              }          }          swap(arr[min_ind],arr[i]);      }   }
0
ramasaimohanakhil1 month ago
//mohanakhildenduluri
class Solution{int  select(int a[],int i){     int min=a[i],index=0;       boolean flag=false;       for(int j=i+1;j<a.length;j++)       {           if(a[j]<min)           {               min=a[j];               index=j;               flag=true;           }       }       if(flag)       {           return index;       }       return -1;           // code here such that selectionSort() sorts arr[]}void selectionSort(int a[], int n){    //code here    int temp=0;       for(int i=0;i<n;i++)       {           int index=select(a,i);           if(index!=-1)           {               temp=a[i];               a[i]=a[index];               a[index]=temp;           }       }}}
0
codewithshoaib191 month ago
//  int  select(int arr[], int i)//  {//         // code here such that selectionSort() sorts arr[]//  }void selectionSort(int arr[], int n){    //code here    for(int i=0;i<n;i++){        int last=n-i-1;        int max=getMax(arr,0,last);        swap(arr,max,last);    }} public static void swap(int[] arr, int s, int e) {       int temp = arr[s];       arr[s] = arr[e];       arr[e] = temp;   }
   public static int getMax(int[] arr, int s, int e) {       int max = s;       for (int i = s; i <= e; i++) {           if (arr[i] > arr[max]) {               max = i;           }       }       return max;   }
0
yash7raut1 month ago
class Solution:    def select(self, arr, i):       # code here       pass      def selectionSort(self, arr,n):       #code here       n = len(arr)       for i in range(n-1):           min_idx = i           for j in range(min_idx+1,n):               if(arr[j]<arr[min_idx]):                   min_idx = j           if(i!=min_idx):               arr[i],arr[min_idx] = arr[min_idx],arr[i]            
+1
vishesh__jhaPremium1 month ago
Time Complexity: 0.0 /2.9
C++
 
 int select(int arr[], int i)   {      //You can AVoid this Function    }       void selectionSort(int arr[], int n)   {        for(int i=0;i<n-1;i++)      {          int Min_Idx=i;                    for(int j=i+1;j<n;j++)          {              if(arr[j]<arr[Min_Idx])                   Min_Idx=j;          }                    swap(&arr[Min_Idx],&arr[i]);      }   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.