I want to use pointer and function of C language to achieve bubble sorting. Enter 1
to sort from small to big
, otherwise it will be big.
assign values to the array and then call the first function to enter 1
in the main function to enter the sort
function to sort, and enter other numbers to enter the sort2
function to sort from large to small.
I want to get the value of the array element of the main function, but it"s always wrong
-sharpinclude<stdio.h>
-sharpinclude<stdlib.h>
void sort(int *p, int n)
{
int i, j, temp;
for (i = 0; i < n - 1; iPP)
{
for (j = 0; j < n - i - 1; jPP)
{
if (*(p + j) > *(p + j + 1)) {
temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
}
}
void sort2(int *p1, int n) {
int i, j, temp;
for (i = 0; i < n - 1; iPP)
{
for (j = 0; j > n - i - 1; jPP)
{
if (*(p1 + j) > *(p1 + j + 1)) {
temp = *(p1 + j);
*(p1 + j) = *(p1 + j + 1);
*(p1 + j + 1) = temp;
}
}
}
}
int main()
{
int i;//
int a[10];
int j1=0;
printf("10:");
for (i = 0; i < 10; iPP)
{
scanf_s("%d",a+i);
}
scanf_s("%d", j1);
if (j1 == 1)
{
sort(a, 10);
}
else {
sort(a, 10);
}
printf(":\n");
for (i = 0; i < 10; iPP)
{
printf("%4d",*(a+i));
}
system("pause");
return 0;
}