Показать сообщение отдельно

  #3  
Старый 05.04.2010, 11:44
comichero92
Новичок
Регистрация: 29.03.2010
Сообщений: 6
С нами: 8484458

Репутация: 0
По умолчанию

Цитата:
Сообщение от Lewis Carroll  
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#define MAX 20
void bubble(int *x,int n);
void shell(int *x,int n);
void insert(int *x,int n);
void main()
{ int num[MAX],i;
int t1,t2;
cout << "Before sort:\n";
for(i=0; i<MAX; i++)
{ num[i]=random(MAX);
cout << num[i] << " ";
}
cout << endl;
t1=GetTickCount();
bubble(num,MAX);
shell(num,MAX);
insert(num,MAX);
t2=GetTickCount();
cout << t2-t1;

cout << "After sort:" << endl;
for(i=0; i<MAX; i++)
cout << num[i] << " ";
cout << endl;

cout << "end";
getch();
}

void bubble(int *x, int n)
{ register int i,j;
int tmp;
for(i=1;i<n;i++)
for(j=n-1;j>=i; j--)
if(x[j-1]>x[j])
{ tmp=x[j-1]; x[j-1]=x[j]; x[j]=tmp; }
}

void insert(int *x, int n)
{ register int i,j;
int tmp;
for(i=1;i<n;i++)
{ tmp=x[i];
for(j=i-1;j>=0 && tmp<x[j]; j--)
x[j+1]=x[j];
x[j+1]=tmp;
}
}
void shell(int *x, int n)
{ register int i,j,gap,k;
int xx;
char a[5]={9,5,3,2,1};
for(k=0;k<5;k++)
{ gap=a[k];
for(i=gap;i<n;i++)
{ xx=x[i];
for(j=i-gap; xx<x[j] && j>=0; j=j-gap)
x[j+gap]=x[j];
x[j+gap]=xx;
}
}
}





переписал программу сортировки в следующем виде...
вылетают непонятные ошибки.. может кто нибудь разъяснить?
Если ты используешь Visual Studio то для твоей программы следующие хедеры:

#include <windows.h> // для работы GetTickCount(); (Windows API function)
#include <conio.h>
#include <iostream>

использовать пространство имен
using namespace std;

я не помню что нужно подключить чтобы заработал random() почему бы не использовать rand() и srand() ?
 
Ответить с цитированием