如何递归冒泡排序x-y坐标关联到x [英] How to recursive bubble sort x-y coordinate associate to x
本文介绍了如何递归冒泡排序x-y坐标关联到x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
#include <stdbool.h>
struct Points {
int X, Y;
};
void swap(int *a, int *b)
{
int t;
t = *b;
*b = *a;
*a = t;
}
void sort(struct Points a[], int n)
{
//You can adapt any of the insertion / selection / bubble sort
if (n == 1) return;
for (int i=0; i<n-1; i++)
if (a[i].X > a[i+1].X)
swap(&a[i].X, &a[i+1].X);
//sort(&a[0], n);
}
void sort(struct Points a[], int n);
int main()
{
int i;
struct Points ptArray[5] = { {11, 34}, {5, 73}, {11, 19}, {13, 5}, {11,
68}};
sort(ptArray, 5);
//The output should match the order given in question
for (i = 0; i < 5; i++){
printf("(%d, %d)\n", ptArray[i].X, ptArray[i].Y);
}
return 0;
}
我的尝试:
当X开始索引时如何将Y与X一起移动
What I have tried:
how to move the Y together with the X when X start indexing
void sort(struct Points a[], int n)
{
if (n == 1) return;
for (int i=0; i<n-1; i++)
if (a[i].X > a[i+1].X)
swap(&a[i].X, &a[i+1].X);
//sort(&a[0], n);
}
样本I / O
Sample I/O
void sort(struct Points a[], int n);
int main()
{
int i;
struct Points ptArray[5] = { {11, 34}, {5, 73}, {11, 19}, {13, 5}, {11,
68}};
sort(ptArray, 5);
//The output should match the order given in question
for (i = 0; i < 5; i++){
printf("(%d, %d)\n", ptArray[i].X, ptArray[i].Y);
}
return 0;
}
输出:
{5,73},{11,19}, {11,34},{11,68},{13,5}
Output:
{5,73} , {11,19}, {11,34}, {11,68}, {13,5}
推荐答案
交换时必须交换洞穴点,不仅是x:
When you swap than you must swap the hole point, not only the x:
swap(Points *a, Points*b);
Make一些输出,以了解发生了什么。您执行的排序不完整。搜索一些教程以了解您的家庭主妇的需求。
Make some output to understand what is going on. You implementation of the sorting is incomplete. Search for some tutorial to understand the needs of your homewrok.
这篇关于如何递归冒泡排序x-y坐标关联到x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文