Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ code for selection sort

#include<bits/stdc++.h>
using namespace std;

int main(){
    //selection sort
    
    int a[5] = {54,69,12,2,89};
    //find minimum in rest of the array and swap with the current index;
    //if(n>1)
    for(int i=0;i<5-1;i++)
    {
        int minloc = i;
        for(int j=i+1;j<5;j++)
        {
            if(a[minloc]>a[j])
            {
               minloc = j;
               
            }
        }
        swap(a[i],a[minloc]);
    }
    for(int i=0;i<5;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}
Comment

selection sort c++

class Practice
{
public:
    void practice1(vector<int> &arr)
    {
        /* Selection sorting */

        int current_min_i = 0;

        for (int i = 0; i < arr.size() - 1; i++)
        {
            current_min_i = i;
            for (int j = i + 1; j < arr.size(); j++)
                if (arr[current_min_i] > arr[j])
                    current_min_i = j;

            swap(arr[i], arr[current_min_i]);
        }

        cout << "Displaying sorted array: ";
        for (int i : arr)
            cout << i << " ";
    }
};

int main()
{
    vector<int> arr{64, 25, 12, 22, 11};

    Practice op;
    op.practice1(arr); 
}
Comment

selection sort c++ algorithm

//Selection sort algorithm
selectionSort(array, size)
  repeat (size - 1) times
  set the first unsorted element as the minimum
  for each of the unsorted elements
    if element < currentMinimum
      set element as new minimum
  swap minimum with first unsorted position
end selectionSort
Comment

selection sort c++

void swap(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}
void Tri_Selection(int tab[],int n)
{
    for (int i=0; i<n; i++)
    {
        for (int j=i+1; j<n; j++)
            if (tab[i] > tab[j])
                swap(tab[i],tab[j]);
    }
}
Comment

selection sort algorithm in cpp

#include <iostream>

void swap(int *xp, int *yp) {
  int temp = *xp;
  *xp = *yp;
  *yp = temp;
}

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

    if (min_idx != i) {
      swap(&arr[min_idx], &arr[i]);
    }
  }
}

void printArray(int arr[], int size) {
  int i;
  for (i = 0; i < size; i++) {
    std::cout << arr[i] << " ";
  }
  std::cout << std::endl;
}

int main() {
  int arr[] = {64, 25, 12, 22, 11};
  int n = sizeof(arr) / sizeof(arr[0]);
  std::cout << "unsorted array: ";
  printArray(arr, n);
  std::cout << std::endl;
  selectionSort(arr, n);
  std::cout << "Sorted array: ";
  printArray(arr, n);
  std::cout << std::endl;

  return EXIT_SUCCESS;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to check is some number is divisible by 3 in c++ 
Cpp :: combination code c++ 
Cpp :: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of the component to the system path if it is installed elsewhere. 
Cpp :: how to iterater map of sets in c++ 
Cpp :: how to traverse a linked list in c++ 
Cpp :: how to convert int to string c++ 
Cpp :: c++ merge sort 
Cpp :: conditional operator in cpp 
Cpp :: c++ measure time in microseconds 
Cpp :: allow cross origin 
Cpp :: max function in c++ 
Cpp :: multiline comment in c++ 
Cpp :: take pieces of a string in c++ 
Cpp :: string length c++ 
Cpp :: adding element in vector c++ 
Cpp :: c++ string contains 
Cpp :: mkdir c++ 
Cpp :: delete a node from binery search tree c++ 
Cpp :: c++ typing animation 
Cpp :: convert string to lpstr 
Cpp :: strlen in c++ 
Cpp :: min element in stl c++ 
Cpp :: pop_back 
Cpp :: detect end of user input cpp 
Cpp :: ue4 float to fstring 
Cpp :: could not find the task c c++ active file 
Cpp :: deep copy c++ 
Cpp :: log in c++ 
Cpp :: currency converter c++ 
Cpp :: unordered_set to vector 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =