StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Friday, March 1, 2013

C++ Selection Sort

 March 01, 2013     Algorithms     No comments   

Selection sort is a simple sorting algorithm. Selection sort involves scanning the list to find the next best number and swapping it with the last element of the list. This article explains the selection sort with an implementation in C++.

Selection Sort Introduction

Selection sort is like: you have face up cards and you can see all at a time. First pick up the maximum numbered card, and then choose the next best maximum number and keeping them in hand in order. Continue till all the cards exhausted. So the idea is (for increasing order sort), scan through the list of numbers and find the maximum and swap it to the last number. Now scan through remaining numbers excluding the last one to find the next best maximum. And now swap with the last number of the remaining numbers. Continue till all the numbers are done.
  1. Best, Average and Worst case performance are O(N^2).
  2. In-place and can be implemented as stable.

Sample implementation of selection sort

#include <iostream>

using namespace std;
const int INPUT_SIZE = 10;

void print(int *input)
{
    for ( int i = 0; i < INPUT_SIZE; i++ )
        cout << input[i] << " ";
    cout << endl;
}

void selectionsort(int* input)
{
    for (int i=INPUT_SIZE-1;i>0;i--)
    {
        //identify which is the max from the remaining numbers.
        int maxPos = 0;
        for (int j=1;j<=i;j++)
        {
            if (input[maxPos]<input[j])
            {
                maxPos = j;
            }
        }
        // Swap it with the last of the remaining numbers.
        int tmp = input[maxPos];
        input[maxPos] = input[i];
        input[i] = tmp;
        cout << "PASS: ";
        print(input);
    }
}

int main()
{
    int input[INPUT_SIZE] = {500, 700, 800, 100, 300, 200, 900, 400, 1000, 600};
    cout << "Input: ";
    print(input);
    selectionsort(input);
    cout << "Output: ";
    print(input);
    return 0;
}
Output:-
Input: 500 700 800 100 300 200 900 400 1000 600 
PASS: 500 700 800 100 300 200 900 400 600 1000 
PASS: 500 700 800 100 300 200 600 400 900 1000 
PASS: 500 700 400 100 300 200 600 800 900 1000 
PASS: 500 600 400 100 300 200 700 800 900 1000 
PASS: 500 200 400 100 300 600 700 800 900 1000 
PASS: 300 200 400 100 500 600 700 800 900 1000 
PASS: 300 200 100 400 500 600 700 800 900 1000 
PASS: 100 200 300 400 500 600 700 800 900 1000 
PASS: 100 200 300 400 500 600 700 800 900 1000 
Output: 100 200 300 400 500 600 700 800 900 1000
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk