Code Monkey home page Code Monkey logo

sorting_algorithm's Introduction

An Algorithm to sort an array of 0s, 1s and 2s in Ascending Order.

This is an algorithm to sort an array of 0s, 1s and 2s in ascending order using time complexity O(n).

This work presents the algorithm used and a directory called src.

The src directory contains the source code and compiled executable file writing in C.

Algorithm

  • Step 1: Iterate (loop) through the array and do the folowing *count all 0s using variable (c_0s) *Count all 1s using variable (c_1s) *Count all 2s using variable (c_2s)

  • Step 2: Update the array by setting start positon variable (pos = 0).

    • Inserting 0s:
      • From O[pos] to O[c_0s -1], insert 0s.
      • Set the starting position of 1s, pos = pos + c_0s.
    • Inserts 1s:
      • From O[pos] to O[(pos+c_1s) -1], insert 1s.
      • Set the starting position of 2s, pos = pos + c_1s.
    • Insert 2s:
      • From O[pos] to O[(pos + c_2s) -1]
  • Step 3. Print the update array.

    • Loop through the updated array and print from O[0] to O[n-1].

sorting_algorithm's People

Contributors

abbottsteve avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.