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

Wednesday, June 8, 2011

C++ Bubble Sort

 June 08, 2011     Algorithms     No comments   

Bubble sort is a simple sorting algorithm where we compare adjacent items and swap them if they are in the wrong order. This is repeatedly done until no exchanges are required. This article provides a sample implementation of bubble sort in C++.

Bubble Sort Introduction

  • Bubble sort is a simple sorting algorithm.
  • Best case time complexity is O(n) when the list is already sorted.
  • Average and Worst case complexity is O(n*n). So it is not recommended for large input sets.
  • Bubble sort involves these simple steps.
    • Compare adjacent items and swap them if they are not in the correct order.
    • Perform the above step until no exchanges are required,

Implementation of bubble sort using C++

#include <iostream>
using namespace std;

static int count = 0;
const int INPUT_SIZE = 10;

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

void bubblesort(int* input)
{
    count++;
    int exchanges = 0;
    for ( int i = 0; i < INPUT_SIZE; i++ )
    {
        if ( input[i] > input[i+1] )
        {
            int tmp = input[i];
            input[i] = input[i+1];
            input[i+1] = tmp;
            exchanges++;
        }
    }
    
    if ( exchanges == 0 ) return;
    cout << "Parse " << count << ":";
    print(input);
    bubblesort(input);
}

int main()
{
    int input[INPUT_SIZE] = {500, 700, 800, 100, 300, 200, 900, 400, 1000, 600};
    cout << "Input: ";
    print(input);
    bubblesort(input);
    cout << "Output: ";
    print(input);
    return 0;
}
OUTPUT:-
Input: 500 700 800 100 300 200 900 400 1000 600
Parse 1:500 700 100 300 200 800 400 900 600 1000
Parse 2:500 100 300 200 700 400 800 600 900 1000
Parse 3:100 300 200 500 400 700 600 800 900 1000
Parse 4: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