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 :: stack class implementation to file unix-style in c++ 
Cpp :: cknuth hash 
Cpp :: Bucket and Water Flow codechef solution in c++ 
Cpp :: prevent getting data from data-tooltip-content tippyjs 
Cpp :: c++ fonksion pointer 
Cpp :: one away coding question 
Cpp :: how to make a vector in c++ 
Cpp :: reference c++ 
Cpp :: max pooling in c++ 
Cpp :: draw line sfml 
Cpp :: c++ count vector elements 
Cpp :: copy constructor c++ syntax 
Cpp :: how to format big numbers with commas in c++ 
Cpp :: how to print items in c++ 
Cpp :: minheap cpp stl 
Cpp :: c/c++ windows api socket wrappers 
Cpp :: has substr c++ 
Cpp :: hide window c++ 
Cpp :: string append at position c++ 
Cpp :: first and last digit of a number in c++ 
Cpp :: for_each c++ 
Cpp :: c++ copy constructor 
Cpp :: pointer to pointer c++ 
Cpp :: convert uppercase to lowercase 
Cpp :: ue4 c++ switch enum 
Cpp :: pthread c++ example with output 
Cpp :: crud with template c++ 
Cpp :: How do you count the occurrence of a given character in a string? c++ 
Cpp :: sort array in descending order c++ 
Cpp :: C++ Modified Data Types List 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =