GTU MATERIAL PROVIDE YOU ALL TYPE OF EDUCATION MATERIAL | DOWNLOAD FREE MATERIAL | EDUCATION SOFTWARES | EXAM ALERTS | EBOOKS | EXAM PAPERS | TIME TABLE | ALL TYPE OF SYLLABUS | MBA | MCA | ENGINEERING | BE | FREE MATERIAL PROVIDE FOR YOU ONLY
GTU MATERIAL PROVIDE YOU ALL TYPE OF EDUCATION MATERIAL | DOWNLOAD FREE MATERIAL | ALL TYPE OF EDUCATION SOFTWARES | EXAM ALERTS | EBOOKS | EXAM PAPERS | TIME TABLE | ALL TYPE OF SYLLABUS | MBA | MCA | ENGINEERING | BE | FREE MATERIAL PROVEDE
SHARE YOUR MATERIAL ALSO.. PLEASE SEND ME YOUR MATERIAL WHO SHARE WITH PEOPLE WE PUBLISH IN GTU MATERIAL WITH YOU NAME.. PLEASE SEND US YOUR NAME, COLLEGE NAME, AND STREAM SO THAT WE CAN PUBLISH WITH YOUR NAME..
C:
-------------------------------------------------------------------------
/* HEAP SORT */
/* HEAP.C */
# include<stdio.h>
void heap_sort(int *, int );
void create_heap(int *, int);
void display(int *, int);
/* Definition of the function */
void create_heap(int list[], int n )
{
int k, j, i, temp;
for(k = 2 ; k <= n; ++k)
{
i = k ;
temp = list[k];
j = i / 2 ;
while((i > 1) && (temp > list[j]))
{
list[i] = list[j];
i = j ;
j = i / 2 ;
if ( j < 1 )
j = 1 ;
}
list[i] = temp ;
}
}
/* End of heap creation function */
/* Definition of the function */
void heap_sort(int list[], int n)
{
int k, temp, value, j, i, p;
int step = 1;
for(k = n ; k >= 2; --k)
{
temp = list[1] ;
list[1] = list[k];
list[k] = temp ;
i = 1 ;
value = list[1];
j = 2 ;
if((j+1) < k)
if(list[j+1] > list[j])
j ++;
while((j <= ( k-1)) && (list[j] > value))
{
list[i] = list[j];
i = j ;
j = 2*i ;
if((j+1) < k)
if(list[j+1] > list[j])
j++;
else
if( j > n)
j = n ;
list[i] = value;
} /* end of while statement */
printf("\n Step = %d ", step);
step++;
for(p = 1; p <= n; p++)
printf(" %d", list[p]);
} /* end for loop */
}
/* Display function */
void display(int list[], int n)
{
int i;
for(i = 1 ; i <= n; ++ i)
{
printf(" %d", list[i]);
}
}
/* Function main */
void main()
{
int list[]={ 0,10,23,64,21,74,95,2,59,44,87,55};
int i, size = 11 ;
clrscr();
/* printf("\n Size of the list: %d", size);
for(i = 1 ; i <= size ; ++i)
{
list[i] = rand() % 100;
}*/
printf("\n Entered list is as follows:\n");
display(list, size);
create_heap(list, size);
printf("\n Heap\n");
display(list, size);
printf("\n\n");
heap_sort(list,size);
printf("\n\n Sorted list is as follows :\n\n");
display(list,size);
getch();
}
--------------------------------------------------------------------------
C++ :
--------------------------------------------------------------------------
// HEAP SORT
// HEAP.CPP
# include<iostream.h>
class heap_s
{
private:
public:
void heap_sort(int *, int );
void create_heap(int *, int);
void display(int *, int);
};
// definition of the function
void heap_s :: create_heap(int list[], int n )
{
for( int k = 2 ; k <= n; ++k)
{
int i = k ;
int temp = list[k];
int j = i / 2 ;
while((i > 1) && (temp > list[j]))
{
list[i] = list[j];
i = j ;
j = i / 2 ;
if ( j < 1 )
j = 1 ;
}
list[i] = temp ;
}
}
// end of heap creation function
// definition of the function
void heap_s :: heap_sort(int list[], int n)
{
for( int k = n ; k >= 2; --k)
{
int temp = list[1] ;
list[1] = list[k];
list[k] = temp ;
int i = 1 ;
int value = list[1];
int j = 2 ;
if((j+1) < k)
if(list[j+1] > list[j])
j ++;
while((j <= ( k-1)) && (list[j] > value))
{
list[i] = list[j];
i = j ;
j = 2*i ;
if((j+1) < k)
if(list[j+1] > list[j])
j++;
else
if( j > n)
j = n ;
list[i] = value;
} // end of while statement
cout<<"\n";
for(int p=1; p<=n; p++)
cout<<" "<<list[p];
} //end for loop
}
void heap_s :: display(int list[], int n)
{
for( int i = 1 ; i <= n; ++ i)
{
cout<<" "<<list[i];
}
}
void main()
{
heap_s sort;
int list[100];
int size ;
cout<<"\n Input the size of the list :";
cin>>size;
for(int i = 1 ; i <= size ; ++i)
{
cout<<"\n Input values for :" <<i<< " : ";
cin>>list[i];
}
cout<<"\n Entered list is as follows:\n";
sort.display(list, size);
sort.create_heap(list, size);
cout<<"\n Heap\n";
sort.display(list, size);
sort.heap_sort(list,size);
cout<<"\n Sorted list is as follows :\n";
sort.display(list,size);
}
C :
-------------------------------------------------------------------------
/* shell.c */
/* shell sort */
#include <stdio.h>
#include <stdlib.h>
void shell_sort(int array[], int size)
{
int temp, gap, i, exchange_occurred;
gap = size / 2;
do {
do {
exchange_occurred = 0;
for (i = 0; i < size - gap; i++)
if (array[i] > array[i + gap])
{
temp = array[i];
array[i] = array[i + gap];
array[i + gap] = temp;
exchange_occurred = 1;
}
} while (exchange_occurred);
} while (gap == gap / 2);
}
void main(void)
{
int values[50], i;
printf("\n Unsorted list is as follows \n");
for (i = 0; i < 50; i++)
{
values[i] = rand() % 100;
printf(" %d", rand() %100);
}
shell_sort(values, 50);
printf("\n Sorted list is as follows \n");
for (i = 0; i < 50; i++)
printf("%d ", values[i]);
}
-------------------------------------------------------------------------
C++
-------------------------------------------------------------------------
// SHELL SORTING
// SHELL.CPP
# include<iostream.h>
#include <stdio.h>
#include <stdlib.h>
class shell
{
private:
int temp, gap, i, swap;
public:
void shell_sort(int *, int );
void display(int *, int);
};
void shell :: shell_sort(int array[], int size)
{
gap = size / 2;
int k =0;
do {
do {
swap = 0;
k++;
for (i = 0; i < size - gap; i++)
if (array[i] > array[i + gap])
{
temp = array[i];
array[i] = array[i + gap];
array[i + gap] = temp;
swap = 1;
}
for(int t=0;t<size; t++)
cout<<" "<<array[t];
cout<<" Swap="<<swap;
cout<<"\n";
} while (swap);
} while (gap = gap / 2);
}
void shell :: display(int list[], int n)
{
cout<<"\n Sorted list is as follows:\n";
for( int i = 0; i < n; i++)
cout<<" " << list[i];
}
void main(void)
{
shell sort;
int list[50];
int number;
cout<<"\n Input the number of elements in the list:";
cin>>number;
for (int i = 0; i < number; i++)
{
cout<<"\n Input the value for the "<< i+1<<" : ";
cin>>list[i];
}
sort.shell_sort(list, number);
sort.display(list,number);
}
// QUICK SORT
# include<iostream.h>
# include <stdlib.h>
class quick
{
private: int temp, low, high, pivot;
public:
void Q_sort(int *, int , int );
void display(int *, int );
};
// sorting function
void quick :: Q_sort(int array[], int first, int last)
{
low = first;
high = last;
pivot = array[(first + last) / 2];
do {
while (array[low] < pivot )
low++;
while (array[high] > pivot)
high--;
if (low <= high)
{
temp = array[low];
array[low++] = array[high];
array[high--] = temp;
}
} while (low <= high);
if (first < high)
Q_sort(array, first, high);
if (low < last)
Q_sort(array, low, last);
}
void quick :: display(int list[], int n)
{
cout<<"\n List after sorting the elements:\n";
for( int i = 1 ; i <= n ; i++)
{
cout<<" "<<list[i];
}
}
void main(void)
{
quick sort;
int list[100];
int number ;
cout<< "\n Input the number of elements in the list:";
cin>> number;
for ( int i = 1; i <= number; i++)
{
cout<<" Input the value for : "<< i <<" : ";
cin>>list[i];
}
sort.Q_sort(list, 1, number);
sort.display(list, number);
}
/* bubble.c */
#include <stdio.h>
#include <stdlib.h>
void bubble_sort(int array[], int size)
{
int temp, i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
void main(void)
{
int values[30], i;
printf("\n Unsorted list is as follows\n");
for (i = 0; i < 10; i++)
{
values[i] = rand() % 100;
printf(" %d", rand()%100);
}
bubble_sort(values, 10);
printf("\n Sorted list is as follows\n");
for (i = 0; i < 10; i++)
printf("%d ", values[i]);
}
------------------------------------------------------------
C++
// BUBBLE SORT
# include<iostream.h>
# include<conio.h>
class bubble
{
private:
public:
void bubble_sort(int , int *); // prototype
void display(int *, int);
};
// definition of function
void bubble :: bubble_sort(int n, int l[])
{
int limit = n - 1 ;
int flag = 1 ;
for(int j = 0 ; j< n - 1; j++)
{
for(int k = 0 ; k< limit - j ; k++)
{
if(l[k] > l[k+1])
{
int temp = l[k];
l[k] = l[k+1];
l[k+1] = temp ;
flag = 0;
}
}
if(flag)
break ;
else
flag = 1;
}
}
void bubble :: display(int list[], int number)
{
for( int i = 0 ; i < number ; i++)
cout<<" "<< list[i];
}
void main()
{
bubble sort;
int number, key, list[200];
clrscr();
cout <<"Input the number of elements in the list:";
cin >> number;
cout <<"\n Number of elements in the list is :"<<number;
for(int i = 0 ; i < number; i++)
{
cout<<"\nInput the elements of the list : "<< i+1<<" : ";
cin >> list[i];
}
cout<<"\n Entered list is as follows:\n";
sort.display(list,number);
sort.bubble_sort(number, list);
cout<<"\n After sorting list is as follows:\n";
sort.display(list, number);
}