Array-03. Bubble Method Sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.