Monthly Archives: July 2014

Selection Sort – GCC Program for Ubuntu

This PPT gives fundamentals of working of selection sort.

Click following link to DOWNLOAD ppt.

Selection Sort Example

For more details of Selection Sort, you may visit the following link.

http://en.wikipedia.org/wiki/Selection_sort

 

Following is the program for Selection Sort Algorithm for GCC (Ubuntu)

File Name: SELECTION.c

//**********************************************************
// Selection Sorting Algorithm Implementation & Analysis.
//**********************************************************

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<unistd.h>
#define SIZE 25000

void sorting(void);
int rand_range(int, int);

long int x[SIZE];
int main()
{
int i;

//Best-Case(Ascending Order)
for(i=0;i<SIZE;i++)
x[i]=i;

printf(“\n**** Best Case ****\n”);
sorting();

//Average-Case(Random Order)
for(i=0;i<SIZE;i++)
x[i]=rand_range(1, SIZE);

printf(“\n\n**** Average Case ****\n”);
sorting();

//Worst-Case(Descending Order)
for(i=SIZE;i>=0;i–)
x[i]=((SIZE+1500000)-i);

printf(“\n\n**** Worst Case ****\n”);
sorting();

return 0;
}

int rand_range(int min_n, int max_n)
{
return rand() % (max_n – min_n + 1) + min_n;
}

void sorting()
{
long int i,j,indx,small;
long int count=0;
clock_t start, end;
start = clock();

for(i=SIZE-1;i>0;i–)
{
//Place the largest number of x[0] through
//x[i] into large and its index into indx
small=x[0];
indx=0;

for(j=1;j<=i;j++)
{
if(x[j]<small)
{
small=x[j];
indx=j;
count++;
sleep(0.1);
}
}

x[indx]=x[i];
x[i]=small;
}

end = clock();
printf(“The time was: %f Sec\nNo.of Comparisons:%ld\n”, (end – start) / 1000000.0,count); // CLOCKS_PER_SEC
}

 

*************************************************************

Following are the commands to run this program:

gcc -o selection SELECTION.c

./selection

 

Insertion Sort – GCC Program for Ubuntu

This PPT gives fundamentals of working of insertion sort.

Click following link to DOWNLOAD ppt.

InsertionSortPPT

For more details of Insertion Sort, you may visit the following link.

http://en.wikipedia.org/wiki/Insertion_sort

 

Following is the program for Insertion Sort Algorithm for GCC (Ubuntu)

File Name: INSERTION.c

//**********************************************************
// Insertion Sorting Algorithm Implementation & Analysis.
//**********************************************************

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<unistd.h>
#define SIZE 25000

void sorting(void);
int rand_range(int, int);

long int x[SIZE];
int main()
{
int i;

//Best-Case(Ascending Order)
for(i=0;i<SIZE;i++)
x[i]=i;

printf(“\n**** Best Case ****\n”);
sorting();

//Average-Case(Random Order)
for(i=0;i<SIZE;i++)
x[i]=rand_range(1, SIZE);

printf(“\n\n**** Average Case ****\n”);
sorting();

//Worst-Case(Descending Order)
for(i=SIZE;i>=0;i–)
x[i]=((SIZE+1500000)-i);

printf(“\n\n**** Worst Case ****\n”);
sorting();

return 0;
}

int rand_range(int min_n, int max_n)
{
return rand() % (max_n – min_n + 1) + min_n;
}

void sorting()
{
int i,k,y;
long int count=0;
clock_t start, end;
start = clock();

/*
Initially x[0] may be thought of as a sorted file of
one element. After each repetition of the following
loop, the element x[0] through x[k] are in order.
*/
for(k=1;k<SIZE;k++)
{
//Insert x[k] into the sorted file
y=x[k];

//Move down 1 position all elements greater than y
for(i=k-1;i>=0 && y<x[i];i–)
{
x[i+1]=x[i];
count++;
sleep(0.1);
}

//Insert y at proper position
x[i+1]=y;
}

end = clock();
printf(“The time was: %f Sec\nNo.of Comparisons:%ld\n”, (end – start) / 1000000.0,count); // CLOCKS_PER_SEC
}

 

*************************************************************************************************************

Following are the commands to run this program:

gcc -o insertion INSERTION.c

./insertion

Bubble Sort – GCC Program for Ubuntu

This PPT gives fundamentals of working of bubble sort.

Click following link to DOWNLOAD ppt.

BubbleSortExample

 

Following is the program for Bubble Sort Algorithm for GCC (Ubuntu)

File Name: BUBBLE.c

//**********************************************************
// Bubble Sorting Algorithm Implementation & Analysis.
//**********************************************************

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<unistd.h>
#define SIZE 25000

void sorting(void);
int rand_range(int, int);

long int x[SIZE];
int main()
{
int i;

//Best-Case(Ascending Order)
for(i=0;i<SIZE;i++)
x[i]=i;

printf(“\n**** Best Case ****\n”);
sorting();

//Average-Case(Random Order)
for(i=0;i<SIZE;i++)
x[i]=rand_range(1, SIZE);

printf(“\n\n**** Average Case ****\n”);
sorting();

//Worst-Case(Descending Order)
for(i=SIZE;i>=0;i–)
x[i]=((SIZE+1500000)-i);

printf(“\n\n**** Worst Case ****\n”);
sorting();

return 0;
}

int rand_range(int min_n, int max_n)
{
return rand() % (max_n – min_n + 1) + min_n;
}

void sorting()
{
int hold,i,j,pass;
int switched=1;
long int count=0;
clock_t start, end;
start = clock();

for(pass=0;pass<SIZE-1 && switched==1;pass++)
{
//outer loop controls the number of passes
switched=0; //initially no interchange
for(j=0;j<SIZE-pass-1;j++)
{
//inner loop for each individual pass
if(x[j]>x[j+1])
{
switched=1;
hold=x[j];
x[j]=x[j+1];
x[j+1]=hold;
count++;
sleep(0.1);
}
}
}
end = clock();
printf(“The time was: %f Sec\nNo.of Comparisons:%ld\n”, (end – start) / 1000000.0,count); // CLOCKS_PER_SEC
}

 

*************************************************************************************************************

Following are the commands to run this program:

gcc -o bubble BUBBLE.c

./bubble