输入一个二维数组A[5][5],将其所有元素排序后按从小到大存放
2个回答
展开全部
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
int compare(const void *a, const void *b)
{
return *(const int*)a > *(const int *)b;
}
int main()
{
int A[5][5];
int i, j;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
A[i][j] = rand()%100;
qsort(A, 25, sizeof(A[0][0]), compare);
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
printf("%d\n", A[i][j]);
return 0;
}
#include <memory.h>
#include <stdlib.h>
int compare(const void *a, const void *b)
{
return *(const int*)a > *(const int *)b;
}
int main()
{
int A[5][5];
int i, j;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
A[i][j] = rand()%100;
qsort(A, 25, sizeof(A[0][0]), compare);
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
printf("%d\n", A[i][j]);
return 0;
}
展开全部
#include <iostream>
using namespace std;
void QuickSort(int *Array, int left, int right){
int l = left, r = right, mid = Array[(l+r)/2];
while (l<=r){
while (Array[l]<mid) l++;
while (Array[r]>mid) r--;
if (l<=r){
swap(Array[l], Array[r]);
l++,r--;
}
}
if (l<right) QuickSort(Array, l, right);
if (r>left) QuickSort(Array, left, r);
}
int main(){
//int a[5][5] = {17,25,3,1,2,4,5,8,23,9,20,7,6,24,10,15,12,22,11,14,19,21,20,13,16};
int A[5][5];
for(int i = 0; i<5; i++)
for(int j = 0; j<5; j++)
cin >> A[i][j];
QuickSort(&A[0][0],0,25);
for (int i = 0; i<5; i++){
for (int j = 0; j<5; j++) cout << A[i][j] << "\t";
cout << endl;
}
system("pause");
}
using namespace std;
void QuickSort(int *Array, int left, int right){
int l = left, r = right, mid = Array[(l+r)/2];
while (l<=r){
while (Array[l]<mid) l++;
while (Array[r]>mid) r--;
if (l<=r){
swap(Array[l], Array[r]);
l++,r--;
}
}
if (l<right) QuickSort(Array, l, right);
if (r>left) QuickSort(Array, left, r);
}
int main(){
//int a[5][5] = {17,25,3,1,2,4,5,8,23,9,20,7,6,24,10,15,12,22,11,14,19,21,20,13,16};
int A[5][5];
for(int i = 0; i<5; i++)
for(int j = 0; j<5; j++)
cin >> A[i][j];
QuickSort(&A[0][0],0,25);
for (int i = 0; i<5; i++){
for (int j = 0; j<5; j++) cout << A[i][j] << "\t";
cout << endl;
}
system("pause");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询