Answers for "what does selection sort do"

6

selection sort

#include <bits/stdc++.h>

using namespace std; 

void selectionSort(int arr[], int n){
    int i,j,min;
    
    for(i=0;i<n-1;i++){
        min = i;
        for(j=i+1;j<n;j++){
            if(arr[j] < arr[min]){
                min = j;
            }
        }
        if(min != i){
            swap(arr[i],arr[min]);
        }
    }
}

int main()  
{  
    int arr[] = { 1,4,2,5,333,3,5,7777,4,4,3,22,1,4,3,666,4,6,8,999,4,3,5,32 };  
    int n = sizeof(arr) / sizeof(arr[0]);  

    selectionSort(arr, n);  

    for(int i = 0; i < n; i++){
        cout << arr[i] << " ";
    }

    return 0;  
}
Posted by: Guest on January-16-2021
1

Selection sort

SelectionSort(List) {
  for(i from 0 to List.Length) {
    SmallestElement = List[i]
    for(j from i to List.Length) {
      if(SmallestElement > List[j]) {
        SmallestElement = List[j]
      }
    }
    Swap(List[i], SmallestElement)
  }
}
Posted by: Guest on April-25-2021
0

selection sort

//selection sort; timecomplexity=O(n^2);space complexity=O(n);auxiliary space complexity=O(1)
#include <iostream>

using namespace std;
void swap(int*,int*);
void selection_sort(int arr[],int n)
{
    for(int i=0;i<n-1;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(arr[i]>arr[j])
            {
                swap(&arr[i],&arr[j]);
            }
        }
    }
}
void display(int arr[],int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

int main()
{
    int n;
    cout<<"enter the size of the array:"<<endl;
    cin>>n;
    int array_of_numbers[n];
    cout<<"enter the elements of the array:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>array_of_numbers[i];
    }
    cout<<"array as it was entered"<<endl;
    display(array_of_numbers,n);
    cout<<"array after sorting:"<<endl;
    selection_sort(array_of_numbers,n);
    display(array_of_numbers,n);
    return 0;
}
void swap(int *a,int *b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
Posted by: Guest on May-25-2021
0

Selection Sort

array([1, 2, 3, 4, 5])
Posted by: Guest on October-09-2021
0

selection sort

#include <iostream>

// findSmallest() function returns the smallest number in the given array
int findSmallest(int* arr, int length) {
	int small = arr[0], smallIndex = 0;
	for (int i = 1; i < length; i++)
		if (arr[i] < small) {
			small = arr[i];
			smallIndex = i;
		}
	return smallIndex;
}

// pop() function remove the smallest number in the given array
void pop(int* arr, int& length, int index) {
	for (int i = index; i < length - 1; i++)
		arr[i] = arr[i + 1];
	length--;
}

// sort() main sorting function that finds and store the smallest number
//			in new array and remove that smallest number
//			and at the end returns the address of new sorted array !
int* sort(int* arr, int length) {
	int* newArr = new int[length], length1 = length;
	for (int i = 0; i < length; i++) {
		int smallest = findSmallest(arr, length1);
		newArr[i] = arr[smallest];
		pop(arr, length1, smallest);
	}
	delete arr;
	arr = nullptr;
	return newArr;
}

int main() {
	int* arr = new int[6]{ 5,5, 3, 6, 2, 10 };
	arr = sort(arr, 6);	// 6 is the length of array
	for (int i = 0; i < 6; i++)
		std::cout << arr[i] << " ";
	return 0;
}
Posted by: Guest on October-17-2021

Browse Popular Code Answers by Language