Array-03. Bubble sort (20) time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Xu Jianchun (Zhejiang University)
The bubbling sort, which sorts n integers from small to large, works like this: compare adjacent two elements from beginning to end, and swap them if the preceding elements are larger than the following elements immediately after them. By scanning again, the last element must be the largest element. The first N-1 elements are then scanned again in the same way. And so on, the order of n number is finished by processing two elements at the end.
This is required for any given K (<n), the output scan after the K-Pass of the intermediate result sequence.
Input format:
The input is given N and K (1<=k<n<=100) in line 1th, and N is the number of integers to be sorted in line 2nd, separated by a space between the numbers.
Output format:
In a row, the output bubble Sort method scans the intermediate result sequence after the K-pass, separated by a space, but with no extra space at the end.
Input Sample:
6 22 3 5 1 6 4
Sample output:
2 1 3 4 5 6
1#include <stdio.h>2#include <math.h>3#include <stdlib.h>4#include <string.h>5 intMain ()6 {7 intN, K, temp, I, J, a[ the];8scanf"%d%d", &n, &k);9 for(i =0; I < n; i++)Tenscanf"%d", &a[i]); One for(i =0; I < K; i++) A for(j =0; J < N-i-1; J + +) - { - if(A[j] > a[j+1]) the { -temp =A[j]; -A[J] = a[j+1]; -a[j+1] =temp; + } - } +printf"%d", a[0]); A for(i =1; I < n; i++) atprintf"%d", A[i]); -printf"\ n"); - return 0; -}
Array-03. Bubble Method Sort