简体   繁体   English

最容易在C中使用int数组排序功能

[英]Easiest to use int array sorting function in C

I am looking for easiest to use array sorting function in C. I am going to teach somebody little bit of C(actually these are common basics to every language). 我正在寻找最容易在C中使用数组排序功能。我将教一些C语言(实际上这些是每种语言的常用基础知识)。 Is there any function for int arrays like Java's 是否有像Java这样的int数组的函数

Arrays.sort(arr);

I have seen qsort, but as I saw it needs additional compare function. 我见过qsort,但是我看到它需要额外的比较功能。

So... implement the function and be done with it... 所以...实现功能并完成它...

int compare_int( const void* a, const void* b )
{
    if( *(int*)a == *(int*)b ) return 0;
    return *(int*)a < *(int*)b ? -1 : 1;
}

const size_t num_elem = 10;
int elements[num_elem] = { 3, 6, 1, 9, 8, 2, 0, 5, 7, 4 };
qsort( elements, num_elem, sizeof(int), compare_int );

Now your lesson about sorting becomes "how does this work"? 现在关于排序的课程变成“这是如何工作的”?

You start by explaining memory layout and arrays. 首先解释内存布局和数组。 You can't do much in C until you know this anyway. 在你知道这一点之前,你不能做很多事情。

Then you explain what a void pointer is and why the qsort function needs to know: 然后你解释一下void指针是什么以及为什么qsort函数需要知道:

  1. The start address of your array 数组的起始地址
  2. The number of elements 元素的数量
  3. The size of each element 每个元素的大小
  4. How to compare the elements 如何比较元素

That leads naturally to the comparison function itself... How to cast and dereference types. 这自然导致比较函数本身...如何转换和取消引用类型。

Finally, if they are grasping the concepts well, you could point out that the fourth parameter to qsort isn't a special case. 最后,如果他们掌握了很好的概念,你可以指出qsort的第四个参数不是特例。 You can say it's perfectly okay to have a pointer to a function and pass that as a parameter to another function. 你可以说有一个指向函数的指针并将其作为参数传递给另一个函数是完全可以的。 It's all about the getting the type of the pointer correct, then the compiler sorts the rest out for you. 这是关于获取指针类型的所有内容,然后编译器会为您排序其余部分。

int (*comparator)(const void*, const void*) = compare_int;
int a = 1, b = 2;
printf( "comparator(%d, %d) = %d\n", a, b, comparator(&a, &b) );

The easiest way, at my first C programming course I've written this without looking for any algorithm online: 最简单的方法,在我的第一个C编程课程中,我已经编写了这个,而无需在线查找任何算法:

for(int i=0; i<N;i++)
{
    for(int j=0;j<N-1;j++)
    {
        if(array[j]<array[j+1])
        {
            int temp=array[j];
            array[j]=array[j+1];
            array[j+1]=temp;
        }
    }
}

I knew that it could be made in less that N*(N-1) iterations, but I didn't know how to calculate the exact number of iterations, so to be sure to sort all elements I made it this way. 我知道它可以在更少的N *(N-1)次迭代中完成,但我不知道如何计算确切的迭代次数,所以要确保按照这种方式对所有元素进行排序。
If you want you can reduce the number of iterations by knowing that at every iteration one element gets sorted, so do the second loop can go from 0 to Ni-1.But I was too lazy to calculate this number and for the professor was ok :-) 如果你想要,你可以通过知道在每次迭代时一个元素被排序来减少迭代次数,所以第二个循环可以从0变为Ni-1。但是我懒得计算这个数字并且教授没问题:-)

If you're just doing this for teaching purposes, why don't you just write your own easy-to-use sort() based on qsort() ? 如果您只是出于教学目的而这样做,为什么不编写基于qsort()的易于使用的sort() qsort() There isn't a standard function that is as simple as you're looking for, so implementing your own is the best option. 没有标准功能就像您正在寻找的那样简单,因此实施您自己的功能是最佳选择。

int compare(const void *a, const void *b) {
    return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b);
}

void sort(int *arr, size_t len) {
    qsort(arr, len, sizeof(int), compare);
}

**If you are reading this you will get an idea about sorting ** **如果您正在阅读本文,您将了解有关排序的信息**

package com.alindal.sort;
import java.util.Scanner;

public class Sort {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        int[] numers;
        System.out.println("Enter the number of elements: ");

        Scanner n=new Scanner(System.in);
        int x=n.nextInt();
        int[] srt=new int[10];
        for(int i=0;i<x;i++)
        {
            srt[i]=n.nextInt();
            }


        System.out.println("The sorted numbers :");
        for(int i=0;i<x-1;i++)
        {
            for(int j=i+1;j<x;j++)
            {

                if(srt[i]>srt[j])
                {
                    int temp=srt[i];
                    srt[i]=srt[j];
                    srt[j]=temp;

                }
                else{
                    srt[i]=srt[i];
                srt[j]=srt[j];
            }

        }

        for(i=0;i<x;i++)
        {
        System.out.println(srt[i]);
        }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM