热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:2D阵列选择排序

篇首语:本文由编程笔记#小编为大家整理,主要介绍了2D阵列选择排序相关的知识,希望对你有一定的参考价值。所以

篇首语:本文由编程笔记#小编为大家整理,主要介绍了2D阵列选择排序相关的知识,希望对你有一定的参考价值。



所以...我想做一个2D选择排序algorythm,但我不知道我做错了什么(C),你能帮帮我吗?

我只需要从第一个数组复制偶数数字到第二个数组,然后通过选择排序对它们进行排序,通常是:

for(x = 0; x for(y = x + 1; y {
if(arr[a] > arr[b]
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}

事情是我无法将其转换为2D这是代码:

void arrcpy(int arrA[3][5], int arrB[3][5])
{
int countx, county;
int countxB = 0, countyB = 0;
int temp;
int tempx, tempy;
int swapped = 0;
for(countx = 0; countx <3; countx++)
{
for(county = 0; county <5; county++)
if(arrA[countx][county] % 2 == 0 && arrA[countx][county] != 0)
{
arrB[countxB][countyB] = arrA[countx][county];
if((countyB) / 4 == 1)
{
countxB++;
countyB = 0;
}
else
countyB++;
}
}
printf("
");
show(arrB);
printf("
");
//Works up to now
for(countx = 0; countx <3; countx++)
for(county = 0; county <5; county++)
if(arrB[countx][county] % 2 == 0)
{
tempx = countx;
tempy = county;
}
countx = county = countxB = 0;
countyB = 1;
//Works up to now
for(countx = 0; countx <3; countx++)
{
for(county = 0; county <5; county++)
{
swapped = 0;
for(countxB = 0; countxB <3; countxB++)
{
if(swapped)
break;
for(countyB = 0; countyB <5; countyB++)
{
if(swapped)
break;
if(arrB[countx][county] {
temp = arrB[countx][county];
arrB[countx][county] = arrB[countxB][countyB];
arrB[countxB][countyB] = temp;
swapped = 1;
}
}
}
}
}
}

答案

坦率地说,我很难遵循你的代码。部分原因是由于不幸的长名选择。我在这里分享Pablo情绪。通常,选择短名称ij进行索引。

请允许我说,应该以可以重用函数的方式编写程序。读取和调试代码的一小部分更容易。 main功能应尽可能短。它应该只调用已经准备好的函数。

在1D阵列的帮助下对2D阵列进行排序在概念上比在适当的位置更简单。我也添加了这个解决方案。

你可能想看看:

#include
#include
#include
#define ROW 3
#define COL 5
void arrcpy(int arrA[ROW][COL], int arrB[ROW][COL])
{
// copy from arrA to arrB ( arrA -> arrB )
// copy only even numbers from the first to the second array but not 0
int i, j;
for(i = 0; i {
for(j = 0; j if(arrA[i][j] % 2 == 0 && arrA[i][j] != 0)
{
arrB[i][j] = arrA[i][j];
}
}
}
void show(char *mess, int arr[ROW][COL], int row, int col )
{
int i;
int j;
printf("%s
", mess);
for(i=0 ;i {
for(j=0; j printf("%d ",arr[i][j]);
printf("
");
}
printf("
");
}
void sort(int arr[ROW][COL], int row, int col)
{
int min,i,j,tmp,y,k,w;
int z=0, q=0;
for(i=0; i {
for(j=0; j {
z = i;
q = j;
min = arr[i][j];
w = j;
for(k=i; k {
for(; w {
if(arr[k][w] {
min = arr[k][w];
z = k;
q = w;
}
}
w = 0;
}
tmp = arr[i][j];
arr[i][j] = min;
arr[z][q] = tmp;
}
}
}
//-------------------------------
void convert2Dto1D(int arr[ROW*COL], int matrix[ROW][COL], int row, int col)
{
// convert 2D array into 1D array
int k=0;
for(int i=0; i for(int j=0; j arr[k++] = matrix[i][j];
}
}
}
void convert1Dto2D(int matrix[ROW][COL], int arr[ROW*COL], int row, int col)
{
// convert 1D array into 2D array
memcpy(matrix, arr, row*col * sizeof(int));
}
void sort1D(int arr[ROW*COL], int row, int col )
{
// sort 1D array
int temp;
for(int i=0; i {
for(int j=0; j {
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//----------------------------------------------------
int main()
{
int arrA[ROW][COL] = {{9,8,2,10,14},{4,5,1,11,0},{6,7,3,12,0}};
int arrB[ROW][COL] = {{19,18,12,110,114},{14,15,11,111,115},{161,17,13,112,113}};
show("arrA:
", arrA, ROW, COL );
show("arrB:
", arrB, ROW, COL );
arrcpy(arrA, arrB);
show("Arr B after copy:
", arrB, ROW, COL );
sort(arrB, ROW, COL) ;
show("ArrB after sorting:
", arrB, ROW, COL );
// Sorting using 1D array
int arr[ROW][COL] = {{9,8,2,10,14},{4,5,1,11,0},{6,7,3,12,0}};
show("--------------------
Array to be sorted:
", arr, ROW, COL );
int result[ROW*COL];
convert2Dto1D(result, arr, ROW, COL);
sort1D(result, ROW, COL );
convert1Dto2D(arr, result, ROW, COL);
show("Sorted array:
", arr, ROW, COL );
return 0;
}

输出:

arrA:
9 8 2 10 14
4 5 1 11 0
6 7 3 12 0
arrB:
19 18 12 110 114
14 15 11 111 115
161 17 13 112 113
Arr B after copy:
19 8 2 10 14
4 15 11 111 115
6 17 13 12 113
ArrB after sorting:
2 4 6 8 10
11 12 13 14 15
17 19 111 113 115
--------------------
Array to be sorted:
9 8 2 10 14
4 5 1 11 0
6 7 3 12 0
Sorted array:
0 0 1 2 3
4 5 6 7 8
9 10 11 12 14

我希望它有所帮助。



推荐阅读
  • A题这题贼水,直接暴力就可以了。用个bool数组记录一下,如果某一天,当前剩下的最大的出现了的话,就输出一段。1#include<stdio.h>2intn;3boolvi ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本题探讨了一种字符串变换方法,旨在判断两个给定的字符串是否可以通过特定的字母替换和位置交换操作相互转换。核心在于找到这些变换中的不变量,从而确定转换的可能性。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了重温Linux内核:互斥和同步相关的知识,希望对你有一定的参考价值。文章目录 ... [详细]
  • Java 中 Writer flush()方法,示例 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • C++实现经典排序算法
    本文详细介绍了七种经典的排序算法及其性能分析。每种算法的平均、最坏和最好情况的时间复杂度、辅助空间需求以及稳定性都被列出,帮助读者全面了解这些排序方法的特点。 ... [详细]
  • 本文介绍如何利用动态规划算法解决经典的0-1背包问题。通过具体实例和代码实现,详细解释了在给定容量的背包中选择若干物品以最大化总价值的过程。 ... [详细]
  • xmpphp测试openfire发布信息
    xmpphp测试openfire发布信息1.先下载xmpphp,地址:https:code.google.compxmpphpdownloadslist2.编写php脚本。<?php ... [详细]
  • 该楼层疑似违规已被系统折叠隐藏此楼查看此楼错误72error:ErroropeningoutputfileC:Users林鑫辰AppDataLocalTemptmpxft_0000 ... [详细]
author-avatar
淘老婆桃桃_267
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有