Bubble Sort,bubblesort
Jun 13, 2016 am 09:21 AMBubble Sort,bubblesort
8 numbers. Sort as ascend.
?
1st loop, compare 7 times (for 8 numbers), and found the largest 8.
2nd loop, compare 6 times (for 7 numbers), and found the largest 7.
. . .
?
1, 7, 8
2, 6, 7
3, 5, 6
4, 4, 5
5, 3, 4
6, 2, 3
7, 1, 2
?
In conclusion:?For sorting 8 numbers, we need an outer loop of 7 times, each time for finding a largest number; and an inner loop from comparing 7 times to comparing 1 time (as in the center column).
?
Implementation in PHP:?
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span><span> bubble sort: </span><span> 3</span> <span> 1. operate directly on the input array (&), not on a copy </span><span> 4</span> <span> 2. sort as ascend </span><span> 5</span> <span> 6</span> <span> a is array </span><span> 7</span> <span> m is length of a </span><span> 8</span> <span> n is times of outer loop, n-i is times of comparing for each outer loop </span><span> 9</span> <span> i/j is for-loop counter </span><span>10</span> <span> w is for value swap </span><span>11</span> <span>*/</span> <span>12</span> <span>function</span> sortBubble(&<span>$a</span><span>){ </span><span>13</span> <span>$m</span> = <span>count</span>(<span>$a</span><span>); </span><span>14</span> <span>$n</span> = <span>$m</span> - 1<span>; </span><span>15</span> <span>for</span>(<span>$i</span>=0; <span>$i</span><<span>$n</span>; <span>$i</span>++<span>){ </span><span>16</span> <span>for</span>(<span>$j</span>=0; <span>$j</span><<span>$n</span>-<span>$i</span>; <span>$j</span>++<span>){ </span><span>17</span> <span>if</span>(<span>$a</span>[<span>$j</span>] > <span>$a</span>[<span>$j</span>+1<span>]){ </span><span>18</span> <span>$w</span> = <span>$a</span>[<span>$j</span><span>]; </span><span>19</span> <span>$a</span>[<span>$j</span>] = <span>$a</span>[<span>$j</span>+1<span>]; </span><span>20</span> <span>$a</span>[<span>$j</span>+1] = <span>$w</span><span>; </span><span>21</span> <span> } </span><span>22</span> <span>else</span><span>{ </span><span>23</span> <span>//</span><span> do nothing</span> <span>24</span> <span> } </span><span>25</span> <span> } </span><span>26</span> <span>//</span><span> see the results after each outer loop </span><span>27</span> <span> // echo implode(', ', $a).'<br />';</span> <span>28</span> <span> } </span><span>29</span> <span>} </span><span>30</span> <span>31</span> <span>$arr</span> = <span>array</span>(9, 5, 2, 7, 3<span>); </span><span>32</span> sortBubble(<span>$arr</span><span>); </span><span>33</span> <span>echo</span> <span>implode</span>(', ', <span>$arr</span><span>); </span><span>34</span> <span>35</span> <span>//</span><span> 2, 3, 5, 7, 9</span> <span>37</span> ?>
?
冒泡排序詳細(xì)注釋:
冒泡排序詳細(xì)注釋:
/* 用冒泡排序法對(duì)一維整型數(shù)組中的十個(gè)數(shù)升序排序 */
#include
#include
int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n");
for(i=0;iscanf("%d",&a[i]);
for(i=0;ifor(j=0;jif(a[j]>a[j+1])
{t=a[j];/* 交換a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
}
printf("The sequence after sort is:\n");
for(i=0;iprintf("%-5d",a[i]);
printf("\n");
system("pause");
return 0;
}
其中i=0時(shí):
j從0開始a[0],a[1]比較大小,把其中的較大者給a[1],然后j++,a[1]和a[2]再比較,再把兩者中的
較大者給a[2],這樣a[0],a[1],a[2]中的最大者已經(jīng)交換到a[2]中,這個(gè)過程繼續(xù),直到j(luò)=10-i-1=9這樣
a[9]中的為10個(gè)數(shù)中的最大數(shù)。
然后i=1時(shí):
由于最大數(shù)已找到并放到a[9]中,所以這一次循環(huán)j最大只需到10-i-1=8,即a[8]即可,再次從j=0開始a[j]和a[j+1]兩兩比較交換,最后次大數(shù)放到a[8]中
然后i++,繼續(xù)...
當(dāng)i=9時(shí)已經(jīng)過9次兩兩比較完成所有排序,i對(duì)于n個(gè)數(shù),只需要進(jìn)行n-1次外循環(huán)的兩兩比較就完成排序。
至于按降序排列只需將if(a[j]>a[j+1])改為if(a[j]
-------------------------------------------------------------------
/* 用改進(jìn)型冒泡排序法對(duì)一維整型數(shù)組中的十個(gè)數(shù)升序排序 */
#include
#include
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;iscanf("%d",&a[i]);
for(i=0;i{ flag=0;
for(j=0;jif(a[j]>a[j+1])
{ t=a[j]; /* 交換a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
flag=1;
}
if(flag==0)break;
}
printf("The sequence after sort is:\n&......余下全文>>
?
/* 用冒泡排序法對(duì)一維整型數(shù)組中的十個(gè)數(shù)升序排序 */
#include
#include
int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n");
for(i=0;iscanf("%d",&a[i]);
for(i=0;ifor(j=0;jif(a[j]>a[j+1])
{t=a[j];/* 交換a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
}
printf("The sequence after sort is:\n");
for(i=0;iprintf("%-5d",a[i]);
printf("\n");
system("pause");
return 0;
}
其中i=0時(shí):
j從0開始a[0],a[1]比較大小,把其中的較大者給a[1],然后j++,a[1]和a[2]再比較,再把兩者中的
較大者給a[2],這樣a[0],a[1],a[2]中的最大者已經(jīng)交換到a[2]中,這個(gè)過程繼續(xù),直到j(luò)=10-i-1=9這樣
a[9]中的為10個(gè)數(shù)中的最大數(shù)。
然后i=1時(shí):
由于最大數(shù)已找到并放到a[9]中,所以這一次循環(huán)j最大只需到10-i-1=8,即a[8]即可,再次從j=0開始a[j]和a[j+1]兩兩比較交換,最后次大數(shù)放到a[8]中
然后i++,繼續(xù)...
當(dāng)i=9時(shí)已經(jīng)過9次兩兩比較完成所有排序,i對(duì)于n個(gè)數(shù),只需要進(jìn)行n-1次外循環(huán)的兩兩比較就完成排序。
至于按降序排列只需將if(a[j]>a[j+1])改為if(a[j]
-------------------------------------------------------------------
/* 用改進(jìn)型冒泡排序法對(duì)一維整型數(shù)組中的十個(gè)數(shù)升序排序 */
#include
#include
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;iscanf("%d",&a[i]);
for(i=0;i{ flag=0;
for(j=0;jif(a[j]>a[j+1])
{ t=a[j]; /* 交換a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
flag=1;
}
if(flag==0)break;
}
printf("The sequence after sort is:\n&......余下全文>>
?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Data structures and algorithms are the basis of Java development. This article deeply explores the key data structures (such as arrays, linked lists, trees, etc.) and algorithms (such as sorting, search, graph algorithms, etc.) in Java. These structures are illustrated through practical examples, including using arrays to store scores, linked lists to manage shopping lists, stacks to implement recursion, queues to synchronize threads, and trees and hash tables for fast search and authentication. Understanding these concepts allows you to write efficient and maintainable Java code.

Function pointer technology can improve code efficiency and reusability, specifically as follows: Improved efficiency: Using function pointers can reduce repeated code and optimize the calling process. Improve reusability: Function pointers allow the use of general functions to process different data, improving program reusability.

Go is an increasingly popular programming language that is designed to be easy to write, easy to read, and easy to maintain, while also supporting advanced programming concepts. Time complexity and space complexity are important concepts in algorithm and data structure analysis. They measure the execution efficiency and memory size of a program. In this article, we will focus on analyzing the time complexity and space complexity in the Go language. Time Complexity Time complexity refers to the relationship between the execution time of an algorithm and the size of the problem. Time is usually expressed in Big O notation

How to implement the bubble sort algorithm in C# Bubble sort is a simple but effective sorting algorithm that arranges an array by comparing adjacent elements multiple times and exchanging positions. In this article, we will introduce how to implement the bubble sort algorithm using C# language and provide specific code examples. First, let us understand the basic principles of bubble sort. The algorithm starts from the first element of the array and compares it with the next element. If the current element is larger than the next element, swap their positions; if the current element is smaller than the next element, keep it

Algorithms are the set of instructions to solve problems, and their execution speed and memory usage vary. In programming, many algorithms are based on data search and sorting. This article will introduce several data retrieval and sorting algorithms. Linear search assumes that there is an array [20,500,10,5,100,1,50] and needs to find the number 50. The linear search algorithm checks each element in the array one by one until the target value is found or the complete array is traversed. The algorithm flowchart is as follows: The pseudo-code for linear search is as follows: Check each element: If the target value is found: Return true Return false C language implementation: #include#includeintmain(void){i

How to write a custom PHP array sorting algorithm? Bubble sort: Sorts an array by comparing and exchanging adjacent elements. Selection sort: Select the smallest or largest element each time and swap it with the current position. Insertion sort: Insert elements one by one into the sorted part.

PHP array sorting algorithm complexity: Bubble sort: O(n^2) Quick sort: O(nlogn) (average) Merge sort: O(nlogn)

The use of data structures and algorithms is crucial in cloud computing for managing and processing massive amounts of data. Common data structures include arrays, lists, hash tables, trees, and graphs. Commonly used algorithms include sorting algorithms, search algorithms and graph algorithms. Leveraging the power of Java, developers can use Java collections, thread-safe data structures, and Apache Commons Collections to implement these data structures and algorithms.
