Skip to main content

non-linear sorting

(1) Heap sort:
A heap is a complete binary tree. A complete binary tree means a tree which has all the nodes present before it enters the next layer. A heap is used in sorting process, by means of recursive rebuilding of the heap again and again and thus sorting the numbers.
A heap is called max heap if in any node, the child nodes are less than or equal to the node. Similarly a min heap is also defined.
As a heap is complete, we use array to store the heap rather than any dynamic or complex data structure. In this case, we store the child node values of a node stored at i, at 2*i+1, 2*i+2.
In this case, we first build the heap once. Then we heapify it again and again replacing the root element each time, therefore, as the root is the highest element, so, we get smaller and smaller heaps and finish the sorting in finite many steps.
Below I have written a program for heap sort which works for integer values and assumes the user will provide correct inputs only.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
void heapify(int A[],int n, int k)

int largest;
int le=2*k+1;
int ri=2*k+2;
if(le<n && A[le]>A[k])
{
largest=le;
}
if(ri<n && A[ri]>A[largest])
{
largest=ri;
}
if(largest!=k)
{
int temp;
temp=A[k];
A[k]=A[largest];
A[largest]=temp;
heapify(A,n,largest);
}
}
void buildheap(int A[],int n)
{
 
for(int i=n/2-1;i>=0;i--)
{
heapify(A,n,i);
}
}
void heapsort(int A[],int n)

int length=n;
int i;
buildheap(A,n);
printf("%d\n",length);
for(i=length-1;i>=0;i--)

int temp;
temp=A[0]; A[0]=A[i]; A[i]=temp;
heapify(A,i,0);
}
}
int main(void) {
int M; int A[100]; int i;
scanf("%d",&M);
for(i=0;i<M;i++)
{
scanf("%d",&A[i]);
}
heapsort(A,M);
for(i=0;i<M;i++)
{
printf("%d ",A[i]);
}

  /*  int i;
    int A[]={5,2,8,4};
    heapsort(A,4);
    for(i=0;i<4;i++)
{
printf("%d ",A[i]);
}
  */
    return 0;
}
(2) Quick sort:
//lemoto process
#include <stdio.h>
void QuickSort(int A[],int low,int high)
{
        if(low<high)
        {
                int p=partition(A,low,high);
            QuickSort(A,low,p-1);
            QuickSort(A,p+1,high);
        }
}
int partition(int A[],int low,int high)
{
        int j;
        int pivot=A[high];
        int i=low-1;
        for(j=low; j<high; j++)
        {
                if(A[j]<=pivot)
                {
                        i++;
                        int temp=A[j];
                        A[j]=A[i];
                        A[i]=temp;
                }
        }
        int tempo=A[i+1];
        A[i+1]=A[j];
        A[j]=tempo;
        return (i+1);
}
int main(void) {
        int M; int A[100]; int i;
 scanf("%d",&M);
 for(i=0;i<M;i++)
 {
  scanf("%d",&A[i]);
 }
 QuickSort(A,0,M-1);
 for(i=0;i<M;i++)
 {
  printf("%d ",A[i]);
 }
        return 0;
}


(3) Counting sort:
This is a bit different from the above two also. This is a non-comparative sort; it does not use comparison to perform the sorting.
Here is the code for it:
#include <stdio.h>
#include <stdlib.h>
void counting_sort(int A[], int length)
{
int i; int frequency[10]; int *SortedArray;
for(i=0;i<10;i++)
{frequency[i]=0;}
SortedArray=(int *)calloc(length,sizeof(int));
for(i=0;i<length;i++)
{SortedArray[i]=-3;}
for(i=0;i<length;i++)
{frequency[A[i]]+=1;}

for(i=1;i<10;i++)
{frequency[i]+=frequency[i-1];}
// for(i=0;i<10;i++)
// {printf("%d\n",frequency[i]);}

int count=0;
while(count<length)

for(i=0;i<10;i++)

if(frequency[i]!=0 && SortedArray[frequency[i]-1]==-3)
{
SortedArray[frequency[i]-1]=i;
frequency[i]-=1;
count+=1;
}
}
}

for(i=0;i<length;i++)
{
A[i]=SortedArray[i];
}

}
int main(void) {
// data is in between 0 to 9
int array[200];
int response[200];
int length; int i;
scanf("%d",&length);
for(i=0;i<length;i++)
{scanf("%d",&array[i]);}
counting_sort(array,length);
// *response=counting_sort(array,length);
for(i=0;i<length;i++)
{printf("%d  ",array[i]);}

return 0;
}

Input:
8
2 4 2 3 9 7 7 1
output:
Success #stdin #stdout 0s 9424KB
1  2  2  3  4  7  7  9 

Comments

Popular posts from this blog

20 Must-Know Math Puzzles for Data Science Interviews: Test Your Problem-Solving Skills

Introduction:   When preparing for a data science interview, brushing up on your coding and statistical knowledge is crucial—but math puzzles also play a significant role. Many interviewers use puzzles to assess how candidates approach complex problems, test their logical reasoning, and gauge their problem-solving efficiency. These puzzles are often designed to test not only your knowledge of math but also your ability to think critically and creatively. Here, we've compiled 20 challenging yet exciting math puzzles to help you prepare for data science interviews. We’ll walk you through each puzzle, followed by an explanation of the solution. 1. The Missing Dollar Puzzle Puzzle: Three friends check into a hotel room that costs $30. They each contribute $10. Later, the hotel realizes there was an error and the room actually costs $25. The hotel gives $5 back to the bellboy to return to the friends, but the bellboy, being dishonest, pockets $2 and gives $1 back to each friend. No...

Deep Learning by Ian GoodFellow, Yoshua Bengio and Aaron courville Review

History: I have been reading deep learning topics from a number of resources like machine learning mastery by Jason Brawlee, Analyticsvidhya, and other blog resources. But the problem has stayed, the problem of inconsistency in the knowledge. Therefore, I have decided to now sit down, and go through a deep learning book thoroughly. And what better name for deep learning other than Ian Goodfellow! So I have found this book named Deep Learning by Ian Goodfellow. Introduction: Plan for this post is reviewing and rewriting the topics from the book, in simpler language and for sharing the pieces of knowledge with my readers. I will update this post continuously as I proceed with the reading also. So ideally this post is broadly about basic to advanced deep learning material discussion. Sponsored Ads Learn deep learning in python with Udemy   Different parts of the book and purpose of them: This book has three parts,which talks about (1) applied mathematics and machine learni...

Pyarabic: python package for Arabic language

 Introduction:  In languages which are non-english and non-european as well, NLP work has progressed slowly in the last few decades because of the lesser number of scholars working on them as well as a lack of global interest in them. But now the time has changed and people from all over the world are collaborating on these lesser explored libraries and they are building resources for working on these languages with the same ease with that of english.  Pyarabic is a package created from such a similar effort which deals with the intricate details of the arabic language and helps processing all kinds of arabic texts. While trying to learn it, being from a non-arab background, I couldn't read lots of parts of the main readthedocs site and had to work my around it. So in this blog post, I will summarize my learnings in english language, so that you can learn it and use the package with much more ease than me. [Credit where credit is due: this article heavily uses the ac...