【问题标题】:Quicksort C - A little different versionQuicksort C - 有点不同的版本
【发布时间】:2017-08-13 20:22:26
【问题描述】:

代码确实运行。这是我正在研究的快速排序的一个有点不同的版本。我遇到了一些重大问题。首先,它将数组中的第一个元素打印为 n:例如(如果您设置 n = 3,即使您将数组中的第一个元素设为 1,它仍然会打印出 3 作为第一个元素)。此外,当您打印出排序后的版本时,它实际上并没有改变任何东西。

n = 3 的示例输入,

设置值 = 8 , 7 , 6

初始输出将等于 3 , 7 , 6

最终输出将等于 3 , 7 , 6

(输出应该是 6 , 7 , 8)

我在网上找不到任何与我的代码相似的代码,所以这可能是新的东西!谢谢。

//preprocessor directives and header files
#include <stdio.h>
#define MAX_ARRAY_SIZE 50

//function prototypes separated by data types
void print_array( int array[], int n );              // Print out the array values
void swap( int array[], int index1, int index2 );    // Swap two array elements. 
void quicksort( int array[], int low, int high );    // Sorting algorithm

int populate_array( int array[] );                  // Fill array with values from user.
int partition( int array[], int low, int high );    // Find the partition point (pivot)

//the main function 
int main(void)
{
    int array[MAX_ARRAY_SIZE];
    //set n = to size of user created size of array

    int n = populate_array(&array[MAX_ARRAY_SIZE]);
    //print the original array to the screen
    print_array(&array[MAX_ARRAY_SIZE], n );
    //perform the algorithm
    quicksort(array, 0, n-1);

    printf("The array is now sorted:\n");
    print_array(&array[MAX_ARRAY_SIZE], n);
    return 0;
}
// *array and array[] are the same...
int populate_array(int array[])
{
    int n = -1;
    printf("Enter the value of n > ");
    scanf("%d", &n);

    if(n > MAX_ARRAY_SIZE)
    {
        printf("%d exceeds the maximum array size. Please try again.\n\n", n);
        populate_array( &array[MAX_ARRAY_SIZE]);
    }
    else if(n < 0)
    {
        printf("%d is less than zero. Please try again.\n\n", n);
        populate_array( &array[MAX_ARRAY_SIZE]);
    }
    else if(n == 0)
    {
        printf("%d Array of size 0? Please don't try this, and... Please try again.\n\n", n);
        populate_array( &array[MAX_ARRAY_SIZE]);
    }
    else
    {
        for(int i = 0; i < n; i++)
            scanf("%d", &array[i]);
    }
    printf("The initial array contains: \n");
    return n;
}

void print_array(int array[], int n)
{

    for(int i = 0; i < n; i++)
        printf("%+5d\n", array[i]);


}

void quicksort(int array[], int low, int high)
{

    if (low < high)
    {
        /* pivot is partitioning index, array[p] is now
           at right place */
        int pivot = partition(array, low, high);

        // Separately sort elements before
        // partition and after partition
        quicksort(array, low, pivot - 1);
        quicksort(array, pivot + 1, high);
    }

}

int partition(int array[], int low, int high)
{
    int pivot = array[high];

    int i = low;
    for (int j = low; j <= high- 1; j++)
    {
        // If current element is smaller than or
        // equal to pivot
        if (array[j] <= pivot)
        {
            swap(array, i, j);
            i = i +1;
        }
    }
    swap(array, i, high);
    return i;
}

void swap(int array[], int index1, int index2)
{
    int temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;

}

【问题讨论】:

  • 你对&amp;array[MAX_ARRAY_SIZE]有什么期望?
  • &amp;array[MAX_ARRAY_SIZE] : 最后一个元素的地址。
  • 1) 这是 C 还是 C++? 2) 代码确实有效。 然后是 我遇到了一些重大问题。 看起来很奇怪。代码是否有效? 3) 当您将&amp;array[MAX_ARRAY_SIZE] 传递给您的函数时——它们正在读取为数组分配的空间——调用未定义的行为。
  • 为了更清楚,所有foo(&amp;array[MAX_ARRAY_SIZE], n);都应该是foo(array, n);
  • 我从您的问题中删除了c++,因为这个问题似乎与 C++ 无关。如果我不正确,并且这个问题是关于 C++ 而不是 C,您可以 edit 您的问题并进行适当的编辑

标签: c algorithm quicksort


【解决方案1】:

这是一个评论很多的答案。我修改了很多代码。

现在这是一个用于用户输入的功能齐全的快速排序数组。

我之前遇到的问题是 &array[MAX_ARRAY_SIZE]。这需要改为“数组”。 &array[MAX_ARRAY_SIZE] 试图访问超出数组实际大小的内存位置。

将其更改为“数组”意味着它正在访问数组中的第一个元素。(如果错误则更正)

我还将填充数组函数更改为健壮的 do-while 循环。而不是试图在其内部重新调用函数。 do-while 循环只允许您更改“n”的值。

/*
Author: Zachary Alberda
*/
//preprocessor directives and header files
#include <stdio.h>
#define MAX_ARRAY_SIZE 50

//function prototypes separated by data types
void print_array( int array[], int n );              // Print out the array values
void swap( int array[], int index1, int index2 );    // Swap two array elements. 
void quicksort( int array[], int low, int high );    // Sorting algorithm

int populate_array( int array[] );                  // Fill array with values from user.
int partition( int array[], int low, int high );    // Find the partition point (pivot)

//the main function 
int main(void)
{
    int array[MAX_ARRAY_SIZE]; //set n = to size of user created size of array

    int n = populate_array(array); //print the original array to the screen

    print_array(array, n ); //print array of size n 

    quicksort(array, 0, n-1); //perform the algorithm low is 0, high is size of array -1.

    printf("The array is now sorted:\n");//Inform user that the array is sorted.

    print_array(array, n);//print the sorted array

    return 0; // exit without errors.
}
// *array and array[] are the same...
int populate_array(int array[])
{

    int n = -1;//initialize variable n(local variable to function populate_array)
    printf("Enter the value of n > ");//inform user of what to input
    scanf("%d", &n);

    /*
        CHECK IF N IS VALID 


        This is a robust do while loop!

        1) Performs the if-statements while 'n' is not valid in a do-while loop.
         -The reason I do this is because it will cause errors
            if the if-statements are individual without the do-while loop.
        2)The program will not crash if you try different combinations
            of inputs for 'n'. :) 
        3)Checks if user input is > MAX_ARRAY_SIZE
        4)Checks if user input is < 0
        5)Checks if user input is == 0
    */
    do
    {
        if(n > MAX_ARRAY_SIZE)
        {
            printf("%d exceeds the maximum array size. Please try again.\n\n", n);
            printf("Enter the value of n > ");
            scanf("%d", &n);
        }
        else if(n < 0)
        {
            printf("%d is less than zero. Please try again.\n\n", n);
            printf("Enter the value of n > ");
            scanf("%d", &n);
        }
        else if(n == 0)
        {
            printf("%d Array of size 0? Please don't try this, and... Please try again.\n\n", n);
            printf("Enter the value of n > ");
            scanf("%d", &n);
        }
    }while(n <= 0 || n > MAX_ARRAY_SIZE);

    //scan in array if user input is valid 
    for(int i = 0; i < n; i++)
        scanf("%d", &array[i]);

    printf("The initial array contains: \n");//Inform user of initial array

    return n;
}

void print_array(int array[], int n)
{
//print array in pre/post order before and after the algorithm.
    for(int i = 0; i < n; i++)
        printf("%+5d\n", array[i]);


}

void quicksort(int array[], int low, int high)
{

    if (low < high)
    {
        /* pivot is partitioning index, array[pivot] is now
           at right place */
        int pivot = partition(array, low, high);

        // Separately sort elements before
        // partition and after partition
        quicksort(array, low, pivot - 1);
        quicksort(array, pivot + 1, high);
    }

}

int partition(int array[], int low, int high)
{
    int pivot = array[high];

    int i = low;

    for (int j = low; j <= high- 1; j++)
    {
        // If current element is smaller than or
        // equal to pivot
        if (array[j] <= pivot)
        {
            swap(array, i, j);
            i = i +1;
        }
    }
    swap(array, i, high);
    return i;
}

void swap(int array[], int index1, int index2)
{
    //swap positions of array index 1 and 2 
    int temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;

}

【讨论】:

  • 一条我不习惯的评论:` //交换数组索引 1 和 2 的位置`应该改为 ` //交换数组索引 1 和索引 2 的位置`(不是 意思是… a[2] = a[1]; … ...)。
猜你喜欢
  • 2013-02-28
  • 2015-05-26
  • 2018-05-06
  • 2023-04-11
  • 2010-09-10
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多