MAP

GTU C PROGRAMS | SHELL SORT PROGRAM

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);
 }

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More