Friday 18 February 2011

GTU C PROGRAM | SELECTION SORT PROGRAME

C.........

/* select.c */
/* selection sort */
#include <stdio.h>
#include <stdlib.h>
void selection_sort(int array[], int size)
{
 int temp, current, j;
 for (current = 0; current < size; current++)
  for (j = current + 1; j < size; j++)
   if (array[current] > array[j])
   {
    temp = array[current];
    array[current] = array[j];
    array[j] = temp;
   }
}
void main(void)
{
 int values[30], i;
 clrscr();
 printf("\n Unsorted list is as follows \n");
 for (i = 0; i < 30; i++)
 {
  values[i] = rand() % 100;
  printf(" %d", rand() %100);
 }
 selection_sort(values, 30);
 printf("\n Sorted list is as follows \n");
 for (i = 0; i < 30; i++)
 printf("%d ", values[i]);
 getch();
}

----------------------------------------------------------------------------
C++.........

// SELECTION SORT
# include<iostream.h>
#include <stdio.h>
#include <stdlib.h>
 class selection
      {
       private:
  int temp, current, j;
       public:
  void selection_sort(int *, int);
  void display(int *, int );
 };
   void selection :: selection_sort(int array[], int size)
   {
   for (current = 0; current < size; current++)
    for (j = current + 1; j < size; j++)
      if (array[current] > array[j])
 {
   temp = array[current];
   array[current] = array[j];
   array[j] = temp;
 }
  }
     void selection :: 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)
 {
   selection sort;
   int list[30];
   int number;
   cout<<"\n Input the number of the elements:";
   cin>>number;
   for (int i = 0; i < number; i++)
      {
 cout<<"\n Input the values for | "<<i+1<<" | ";
 cin>>list[i];
      }
       sort.selection_sort(list, number);
       sort.display(list, number);
    }

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms