我对冒泡排序很好奇,所以我创建了一个函数,它接受用户输入并将值存储在数组的位置中,但它不断打印出一些垃圾值。
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
void sort(int*z);
void swap(int* element1Ptr,int* element2Ptr);
int main(void)
{
int number[10];
int input;
int* sorting;
sorting = number;
printf("Please enter a number less than 10 digits long");
scanf_s("%d", &input);
for (int i=0; i<10;i++)
{
number[9-i]=input%10;
input/=10;
}
printf("\n");
sort(sorting);
printf("%d\n",number[0]);
}
我有错误的气泡排序代码还是我传递了错误的变量?
void sort(int* z)
{
int pass; /* pass counter */
int j; /* comparison counter */
/* loop to control passes */
for ( pass = 0; pass < 11; pass++ )
{
/* loop to control comparisons during each pass */
for ( j = 0; j < 10; j++ )
{
/* swap adjacent elements if they are out of order */
if ( z[ j ] > z[ j + 1 ] )
{
swap( &z[ j ], &z[ j + 1 ] );
} /* end if */
} /* end inner for */
} /* end outer for */
}/* end function bubbleSort */
void swap(int* element1Ptr,int* element2Ptr)
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
} /* end function swap */