Category Archives: Lecture Notes

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

 

Exploring Graphs – PPT

Click following link to download Exploring Graphs PPT.

Exploring Graphs – PPT

Dynamic Programming – Lecture Note

Download lecture note for Dynamic Programming from following link:

Dynamic Programming-PPT

Activity Selection Problem & Job Scheduling Problem – Lecture Note

Click on the following link to download the PDF of Activity Selection Problem & Job Scheduling Problem.

Activity Selection Problem & Job Scheduling Problem

Binary Search Algorithm – PPT

Click on the following link to download the PPT of Binary Search Algorithm.

BinarySearchAlgorithm