Giter Club home page Giter Club logo

heapsort's Introduction

HeapSort

Implementing HeapSort in C++

Problem Statement:

  1. Given a random array of N numbers, convert it into a heap.

  2. The null nodes are indicated by -1 and the null nodes are located at any place in the array. Make this into a heap.

  3. Sort the heap using HeapSort

  4. Display the array after the heap is converted from the array and after HeapSort.

Algorithm

Algorithm:

The algorithm for the given problem statement goes as follows: Note: The implemented heap is a Max Heap

  1. In the first step, the array is filtered and the null nodes are removed and stored in an array called heap[].

After that, the value property of heap is maintained, by using Floyd’s method for building a heap.

Start from the n/2nd element and compare the values of both its children

Case 1: When the parent value is greater than both its children, continue Case 2: When the parent is smaller than right (or) left child Case 3: When the parent is smaller than both its children

  1. After the cases, decrease n by 1 and continue till n = 1 Use a function check() and start from the first element upto the middle and check the value of all children of a given node.

For HeapSort,

  1. Swap the 1st element and the a[0]th element of the array Reduce the value of a[0] Call the check() function Repeat until a[0] is 1

The final array produced will be sorted in ascending order.

heapsort's People

Contributors

vigviswa avatar

Watchers

James Cloos avatar  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.