С++
подскажите,пожалуйста.....нужно написать БЫСТРУЮ(quick, qs) сортировку....сумм эзначений элементов массива по возрастанию в столбце .[codebox] /*дана матрица ar [row,col]. Упорядочить каждый столбец матрицы по возрастанию суммы значений элементов матрицы.
5 разных способов. способы сортировок: пузырьковой, отбора, вставки, Шелла, быстрой сортировки.
Вывести на экран неупорядоченную и упорядоченную матрицы
Вывести на экран число сравнений и перестановок переменных в каждом методе сортировки*/
#include "stdafx.h"
#include <windows.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
HANDLE hStdout;
//---------------------------------------------------------------------------------
void bubble(int **summ,int **ar,const int row,const int col);
void select(int **summ,int **ar,const int row,const int col);
void insert(int **summ,int **ar,const int row,const int col);
void shall(int **summ,int **ar,const int row,const int col);
void quick(int **summ,int **ar,const int row,const int col);
void qs(int **summ,int **ar, int first, int last, const int row,const int col);
int ** newarr(int row, int col);
int ** aclone(int **ar,const int row,const int col);
void printAr(int ** ar, int row, int col);
//----------------------------------------------------------------------------------
int main (void)
{
int row, col, t, b, c, i, j;
int **summ, **summ1, **ar,**ar1, **ts, **ta;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, BACKGROUND_INTENSITY);
SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN);
srand(time(0));
SetConsoleTextAttribute(hStdout, 10);
cout<<"Vveddite kolichestvo strok > 0\n";
cin>>row;
cout<<"Vvedite kolichestvo ctolbtsov> 0\n";
cin>>col;
SetConsoleTextAttribute(hStdout, 15);
cout<<"--------------------------------------------------------------------------------";
printf("\n");
SetConsoleTextAttribute(hStdout, 11);
cout<<"Isxodnaya matritsa:\n";
printf("\n");
ar=new int* [row];
for (i=0; i<row; i++)
ar
=new int[col];
ar1=new int* [row];
for (i=0; i<row; i++)
ar1=new int[col];
for(i = 0;i < row;++i)
{
ar = new int[col];
for(j = 0;j < col;++j)
ar[j] = rand() % 999;
}
for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
{
printf ("%-4d",ar[j]);
printf(" ");
}
printf("\n");
}
printf("\n");
//--------------------------------------------------------------------------------
summ=new int* [row];
for (i=0; i<row; i++)
summ=new int[col];
summ1=new int* [row];
for (i=0; i<row; i++)
summ1=new int[col];
for (j=0; j<col; j++)
{
for (i=0; i<row; i++)
{
t=ar[j]/100;
b=(ar[j]-t*100)/10;
c=(ar[j]-t*100-b*10);
summ[j]=t+b+c;
}
}
printf("\n");
SetConsoleTextAttribute(hStdout, 15);
cout<<"--------------------------------------------------------------------------------";
SetConsoleTextAttribute(hStdout, 12);
printf("\n");
printf("Summi elementov:\n");
printf("\n");
printAr(summ,row,col);
//-------------------------------------------------------------------------------------------
SetConsoleTextAttribute(hStdout, 15);
cout<<"--------------------------------------------------------------------------------";
SetConsoleTextAttribute(hStdout, 2);
printf("Sortirovka puzirkom:\n");
ta = aclone (ar,row,col);
ts = aclone (summ,row,col);
bubble(ts, ta, row,col);
printf("\n");
SetConsoleTextAttribute(hStdout, 15);
cout<<"--------------------------------------------------------------------------------";
printf("\n");
SetConsoleTextAttribute(hStdout, 9);
printf("\n");
printf("Sortirovka vstavkami:\n");
ta = aclone (ar,row,col);
ts = aclone (summ,row,col);
insert(ts, ta,row,col);
printf("\n");
SetConsoleTextAttribute(hStdout, 15);
cout<<"--------------------------------------------------------------------------------";
printf("\n");
SetConsoleTextAttribute(hStdout, 14);
printf("\n");
printf("Sortirovka otborom:\n");
ta = aclone (ar,row,col);
ts = aclone (summ,row,col);
select(ts, ta,row,col);
printf("\n");
SetConsoleTextAttribute(hStdout, 15);
cout<<"--------------------------------------------------------------------------------";
printf("\n");
SetConsoleTextAttribute(hStdout, 7);
printf("\n");
printf("Sortirovka metodom Shella:\n");
ta = aclone (ar,row,col);
ts = aclone (summ,row,col);
shall(ts, ta,row,col);
printf("\n");
SetConsoleTextAttribute(hStdout, 15);
cout<<"--------------------------------------------------------------------------------";
printf("\n");
SetConsoleTextAttribute(hStdout, 13);
printf("\n");
printf("Bistraya sortirovka:\n");
ta = aclone (ar,row,col);
ts = aclone (summ,row,col);
quick(ts, ta,row,col);
printf("\n");
SetConsoleTextAttribute(hStdout, 15);
cout<<"--------------------------------------------------------------------------------";
cin >> i;
cout<<"Press any key";
getch();
}
//--------------------------------------------------------------------------------
void bubble(int **summ,int **ar,const int row,const int col)
{
int i,j,w,t,sravn,perest;
bool b;
sravn=0;
perest=0;
do
{
b=false;
for (j=0; j<col; j++)
for (i=0; i<row-1; i++)
if (summ[j]>summ[i+1][j])
{
w=ar[j];
ar[j]=ar[i+1][j];
ar[i+1][j]=w;
t=summ[j];
summ[j]=summ[i+1][j];
summ[i+1][j]=t;
b=true;
perest++;
sravn++;
}
else {sravn++;}
}
while (;
printAr(ar,row,col);
printf("\n");
printf("sravn %d; perest %d\n",sravn,perest);
}
void select(int **summ,int **ar,const int row,const int col)
{
int i,j,w,t,m,sravn,perest;
sravn=0;
perest=0;
for (j=0; j<col; j++)
for (m=0; m<row-1; m++)
for (i=m+1; i<row; i++)
{
if (summ[j]<summ[m][j])
{
w=ar[m][j];
ar[m][j]=ar[j];
ar[j]=w;
t=summ[m][j];
summ[m][j]=summ[j];
summ[j]=t;
perest++;
sravn++;
}
else
{sravn++;}
}
printAr(ar,row,col);
printf("\n");
printf("sravn %d; perest %d\n",sravn,perest);
}
void insert(int **summ,int **ar,const int row,const int col)//сортировка вставками
{
//вставка.........................................................................
........................
int i,j,e,d,k, sravn,perest;
sravn=0;
perest=0;
for (j=0; j<col;j++)
{
for (i=0; i<row; i++)
{
sravn++;
e=summ[j];
d=ar[j];
for (k = i-1; k >= 0 && e < summ[k][j]; k--)
{
sravn++;
ar[k+1][j]= ar[k][j];
summ[k+1][j] = summ[k][j];
}
summ [k+1][j]=e;
ar [k+1][j]=d;
perest++;
}
}
printAr(ar,row,col);
printf("\n");
printf("sravn %d; perest %d\n",sravn,perest);
}
void shall(int **summ,int **ar,const int row,const int col)
{
int i,j,w,t,p,m,perest;
long sravn;
sravn=0;
perest=0;
for (j=0; j<col; j++)
for (i=0; i<row-1; i++)
while(summ[j]>summ[i+1][j])
{
for (m=3; m>0; m--)
for (p=0; p<row-m; p++)
if (summ[p][j]>summ[m+p][j])
{
w=ar[p][j];
ar[p][j]=ar[p+m][j];
ar[p+m][j]=w;
t=summ[p][j];
summ[p][j]=summ[p+m][j];
summ[p+m][j]=t;
perest++;
sravn++;
}
else
{sravn++;}
}
printAr(ar,row,col);
printf("\n");
printf("sravn %d; perest %d\n",sravn,perest);
}
void qs(int **summ,int **ar, int l, int r, const int row,const int col)//быстрая сортировка
{
int j,val,h,sravn,perest;
sravn=0;perest=0;
for (j=0;j<col;j++)
{
if (l<r)
{ h=1;
if (l!=r)
{
if ((summ[l][j]>summ[r][j])&(true))
{
val=l;
l=r;
r=val;
val=summ[l][j];
summ[l][j]=summ[r][j];
summ[r][j]=val;
val=ar[l][j];
ar[l][j]=ar[r][j];
ar[r][j]=val;
false;
r=r-h;
perest++;
sravn++;
}
else
{
sravn++;
r=r+h;
}
}
else
{
qs(summ,ar,l,l-1,row,col);
qs(summ,ar,l+1,r,row,col);
}
}
}
}
void quick(int **summ, int **ar,const int row,const int col)//быстрая сортировка
{
int sravn,perest;
sravn=0;
perest=0;
qs(summ, ar, 0, row-1, row,col);
printAr(ar,row,col);
printf("\n");
printf("sravn %d; perest %d\n",sravn,perest);
}
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
int ** newarr(int row, int col)
{
int ** ar, i;
ar = new int* [row];
for (i=0;i<row;i++)
{
ar = new int [col];
}
return ar;
}
int ** aclone(int **ar,const int row,const int col)
{
int i,j,**clone;
clone = newarr(row,col);
for (i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
clone[j]=ar[j];
}
}
return clone;
}
void printAr(int ** ar, int row, int col)
{
int i,j;
for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
{
printf ("%-4d",ar[j]);
}
printf("\n");
}
}[/codebox]