Array; Bubble sort

 1 package com.java7;
 2
 3 class Bubble {
 4     public static void main(String[] args) {
 5         int nums[] = { 99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49 };
 6         int a, b, t;
 7         int size;
 8
 9         size = 10; // number of elements to sort
10
11         // display original array
12         System.out.print("Original array is: ");
13         for(int i = 0; i < size; i++)
14             System.out.print(" " + nums[i]);
15         System.out.println();
16
17         for(a = 1; a < size; a++)
18             for(b = size - 1; b >= a; b--) {
19                 if(nums[b-1] > nums[b]) { // if out of order
20                     // exchange elements
21                     t = nums[b-1];
22                     nums[b-1] = nums[b];
23                     nums[b] = t;
24                 }
25             }
26
27         // display sorted array
28         System.out.print("Sorted array is: ");
29         for(int i = 0; i < size; i++)
30             System.out.print(" " + nums[i]);
31         System.out.println();
32
33     }
34 }
时间: 2024-08-04 05:37:27

Array; Bubble sort的相关文章

HDU 5775:Bubble Sort(树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is the code of Bubble Sort in C++. for(int i=1;i<=N;++i) for(int j=N,t;j>i;—j) if(P[j-1] > P

hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 636    Accepted Submission(s): 378 Problem Description P is a permutation of the

Bubble Sort

referrence: GeeksforGeeks Bubble sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. Example:First Pass:( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elem

HDU - 5775 Bubble Sort

P is a permutation of the integers from 1 to N(index starting from 1). Here is the code of Bubble Sort in C++. for(int i=1;i<=N;++i) for(int j=N,t;j>i;-j) if(P[j-1] > P[j]) t=P[j],P[j]=P[j-1],P[j-1]=t; After the sort, the array is in increasing o

hdu 5775 Bubble Sort (树状数组)

Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 359 Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is t

HDU 5775 Bubble Sort(冒泡排序)

HDU 5775 Bubble Sort(冒泡排序) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 P is a permutation of the integers from 1 to N(index starting from 1).Here is the code of Bubble Sort in C++.

HDU 5775 Bubble Sort(树状数组)

题目链接:HDU 5775 题面: Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 709    Accepted Submission(s): 418 Problem Description P is a permutation of the integers from 1 to N(index startin

经典排序算法 - 冒泡排序Bubble sort

 原文出自于 http://www.cnblogs.com/kkun/archive/2011/11/23/bubble_sort.html 经典排序算法 - 冒泡排序Bubble sort 原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换,直到倒数第二位时结束,其余类似看例子 例子为从小到大排序, 原始待排序数组| 6 | 2 | 4 | 1 | 5 | 9 | 第一趟排序(外循环) 第

Java中的经典算法之冒泡排序(Bubble Sort)

Java中的经典算法之冒泡排序(Bubble Sort) 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后.然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后.重复第一趟步骤,直至全部排序完成. 举例说明:要排序数组:int[] arr={6,3,8,2,9,1}; 第一趟排序: 第一次排序:6和3比较,6大于3,交换位置: