指標_bubble sort

 avatar
user_3763047219
c_cpp
2 years ago
515 B
2
Indexable
Never
#include <iostream>
#include <stdio.h>

void bubble(int *arr);


void bubble(int *arr)
{
	for (int i = 4; i >=1; i--) {
		for (int j = 0; j <= i-1; j++) {
			if (*arr > *(arr + 1)) {
				int temp = *arr;
				*arr = *(arr + 1);
				*(arr + 1) = temp;
			}
			arr++;
		}
		arr = arr - i;
	}

	for (int i = 0; i < 4; i++) {
		printf("%d ", arr[i]);
	}
	printf("%d",arr[4]);
}


int main()
{
	int arr[5];
	int i;
	for (i = 0; i < 5; i++)
	{
		scanf("%d", &arr[i]);
	}
	bubble(arr);
}