You are here:

C++/Programming sorting algorithm

Advertisement


Question
Hi sir!  It is difficult to write my reply in the reply section.  Anyways, the code you made didn't work... it's ok.  I'll use another one
because it seems to be complicated.

 I'll use the recursive selection sort.
void place_largest (int array[], int n)
{
int temp, j, max_index);
max_index = n - 1;

for (j = n - 2; j >= 0; --j)
if (array[j] > array[max_index])
  max_index = j;

if (max_index != n-1)
{
 temp = array[n-1];
 array[n-1] = array[max_index];
 array[max_index] = temp;
}
}

void selectsort (int array[], int n)
{
if (n > 1)
 {
  place_largest (array, n);
  selectsort (array, n - 1);
 }
}

 #include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <iostream.h>

#define NUM_ELEMENTS 350

#define CLIP_ON 1

typedef  int   array [NUM_ELEMENTS];


void draw_arrow(int x, int y);
void selection();
void start_time();
void end_time();

struct dostime_t t,reset;

array a,b,c;
int col,pass,i,hold,j,k,begin,end;
unsigned size;
void *buf;

int main(void)
{
  /* request autodetection */
  int gdriver = DETECT, gmode, errorcode;

  /* initialize graphics and local variables */
  initgraph(&gdriver, &gmode, "");

  /* read result of initialization */
  errorcode = graphresult();
  if (errorcode != grOk)  /* an error occurred */
  {
     printf("Graphics error: %s\n", grapherrormsg(errorcode));
     printf("Press any key to halt:");
     getch();
     exit(1); /* terminate with an error code */
  }

randomize();
int ctr,ctr2;
ctr2=250;
for (ctr = 0; ctr < NUM_ELEMENTS ; ctr++){
 a[ctr] = ctr2;
 --ctr2;
}
setviewport(200,55,600,345,CLIP_ON);

col = 0;
for (ctr = 0; ctr <NUM_ELEMENTS; ctr++)
{
bar(col,a[ctr],col,a[ctr]);
col++;
}

 start_time();

 for (pass = 1; pass <=NUM_ELEMENTS -1 ;pass++)
  for (col = 0 ; col <=NUM_ELEMENTS -2 ; col++)
      if ( a[col] > a[col+1] ) {
      j=col+1;
      k=col+2;
      hold = a[col];

      //get the first image
      size = imagesize(col,a[col],col,a[col]);
      buf = malloc(size);
      getimage(col,a[col],col,a[col],buf);
      // erase the first image
  putimage(col,a[col],buf,XOR_PUT);
      // move the first image
      for (int ctr = col;ctr<= col+1 ; ctr++)
      { putimage(ctr,a[col],buf,XOR_PUT);
   putimage(ctr,a[col],buf,XOR_PUT);
      }

   putimage(ctr,a[col],buf,XOR_PUT);
   a[col] = a[col + 1];
      //get the second image
      size = imagesize(k,a[k],k,a[k]);
      buf = malloc(size);
      getimage(k,a[k],k,a[k],buf);
      // erase the second image
  putimage(k,a[k],buf,XOR_PUT);
      // move the second image
      for ( ctr = k;ctr>= j ; ctr--)
      { putimage(ctr,a[k],buf,XOR_PUT);
   putimage(ctr,a[k],buf,XOR_PUT);
      }

   putimage(ctr,a[k],buf,XOR_PUT);

   a[col]=a[col+1];
   a[col+1]=hold;
}
end_time();
  delay (5000);
  closegraph();
  cout << end << "seconds ";
  getch();
  return 0;
}

void start_time()
{
reset.hour    = 1;
reset.minute  = 0;
reset.second  = 0;
begin = reset.second;
reset.hsecond = 0;
_dos_settime(&reset);
}

void end_time()
{
 _dos_gettime(&t);
 end = t.second - begin;
}


The program above when run, brings your screen to black... you will see elements arranged in best (or worst) order and that looks like a line graph sloping upward.  What happens here is that the program sorts it until it looks like a line graph sloping downward.  

Answer
As I had typed directly on the notepad last time (sorry for that), there were a few mistakes.
Please see the following code which is working fine (tested):


#include <iostream>
using namespace std;
int arr[20];

int main ()
{
  int x = 10;
  int i, j;
  for (i = 0 ; i < x; ++i)
  {
     cout << "Enter the number" << endl;
     cin >> arr[i];
  }
  int swap = 1;
  while (swap)
  {
     for (i = 0 ; i < x; ++i)
     {
        for (j = i ; j < x; ++j)
        {
         if (arr[i] > arr[j])
         {
         swap =swap || 1;
         arr[i] ^=arr[j];
         arr[j] ^=arr[i];
         arr[i] ^=arr[j];
         }
         else
         {
         swap =swap && 0;
         }
        }
     }
     
     for (i = x-1 ; i >=0; --i)
     {
        for (j = i  ; j >=0; --j)
        {
         if (arr[i] < arr[j])
         {
         swap = swap || 1;
         arr[i] ^=arr[j];
         arr[j] ^=arr[i];
         arr[i] ^=arr[j];
         }
         else
         {
         swap = swap && 0;
         }
        }
     }
  }
  cout << "here are you " << endl;
  for (i = 0 ; i < x; ++i)
  {
     cout << arr[i] << endl;
  }
  cin >> i;
  return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Dharmender Rai

Expertise

I can answer general and system level C/C++ questions.

Experience

I have 5 years of experience in C++.

©2016 About.com. All rights reserved.