Skip to main content

C Sample: Bubble, Selection & Insertion Sort [o(n2)]


#include<stdio.h>
#include<conio.h>
int list[8]={82,42,49,8,92,25,59,52};


void declare()
{
int list[8]={82,42,49,8,92,25,59,52};
printf("\n");
}


void BubbleSort()
{
int i,j,k;
declare();
printf("\n\t\tBUBBLE SORT\n");
for(i=0;i<=7;i++)
{
for(j=0;j<=7;j++)
{
if(list[i]<list[j])
{
k=list[j];
list[j]=list[i];
list[i]=k;
}
}
printf("\nPhase :: %d",i);
display();
}
}
void SelectionSort()
{
int i,j,f;
declare();
printf("\n\t\tSELECTION SORT\n");
for(i=0;i<=7;i++)
{
for(j=i+1;j<=7;j++)
{
if(list[i]>list[j])
{
f=list[j];
list[j]=list[i];
list[i]=f;
}
}
printf("\nPhase :: %d",i);
display();
}
}
void InsertionSort()
{
int i,j,k;
declare();
printf("\n\t\tINSERTION SORT\n");
for(i=0;i<=7;i++)
{
for(j=0;j<=i;j++)
{
if(list[j]>list[i])
{
k=list[i];
list[i]=list[j];
list[j]=k;
}
}
printf("\nPhase :: %d",i);
display();
}
}


display()
{
int i;   printf("\t");
for(i=0;i<=7;i++)
{
printf("%d ",list[i]);
}
getch();
}




void main(void)
{
clrscr();
BubbleSort();
SelectionSort();
InsertionSort();
}

Comments

Popular posts from this blog

Sample : String Reverse In Java

import java.io.*; class test {     public static void main(String args[])     {         DataInputStream in=new DataInputStream(System.in);         try         {             String text;             System.out.println("\nEnter The Text");             text=in.readLine();             System.out.println("\nIn Reverse Order::\n");             for(int i=text.length()-1;i>=0;i--)             {                 System.out.print(text.charAt(i));             }          ...

Getting Browser Information in javaScript

<html> <head> <script> function getBrowserName() { tag_link.innerHTML="You are surfing internet through  "+'<b>'+navigator.appName+'</b>'+" browser family." } </script> </head> <body onLoad="getBrowserName()"> <center> <p id="tag_link" style="font-size:30"></p> </center> </body> </html>