国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Selection Sort,selectionsort
C語言 選擇排序 輸入k個數(shù)字,遞減輸出的selection_sort(array, k)程序
Home php教程 php手冊 Selection Sort,selectionsort

Selection Sort,selectionsort

Jun 13, 2016 am 09:21 AM
sort

Selection Sort,selectionsort

Red is current min. Yellow is sorted list. Blue is current item.?(picture from wikipedia, a little too fast)

?

10 numbers. Sort as ascend.

?

1. Find the min of the 10 and switch it with a[0], requires 9 times of compare

2. Find the min of the rest 9 and switch it with a[1], requires 8 times of compare

. . .

?

1, 10, 9

2, 9, 8

3, 8, 7

4, 7, 6

5, 6, 5

6, 5, 4

7, 4, 3

8, 3, 2

9, 2, 1

?

In conclusion: For 10 numbers, we need 9 times of finding the min, each has one-short amount of numbers to compare.

?

Implementation in PHP:

<span> 1</span> <?<span>php
</span><span> 2</span> <span>/*</span><span> selection 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, which is finding min of the rest
</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>    min is min
</span><span>12</span> <span>    sub is index of array
</span><span>13</span> <span>*/</span>
<span>14</span> <span>function</span> sortSelection(&<span>$a</span><span>){
</span><span>15</span>     <span>$m</span> = <span>count</span>(<span>$a</span>);
<span>16</span>     <span>$n</span> = <span>$m</span> - 1;
<span>17</span>     <span>$min</span><span>;
</span><span>18</span>     <span>$sub</span>;
<span>19</span>     <span>for</span>(<span>$i</span>=0; <span>$i</span><<span>$n</span>; <span>$i</span>++<span>){
</span><span>20</span>         <span>$min</span> = <span>$a</span>[<span>$i</span>];
<span>21</span>         <span>for</span>(<span>$j</span>=<span>$i</span>; <span>$j</span><<span>$m</span>; <span>$j</span>++){
<span>22</span>             <span>if</span>(<span>$a</span>[<span>$j</span>] < <span>$min</span><span>){
</span><span>23</span>                 <span>$min</span> = <span>$a</span>[<span>$j</span><span>];
</span><span>24</span>                 <span>$sub</span> = <span>$j</span><span>;
</span><span>25</span> <span>            }
</span><span>26</span>             <span>else</span><span>{
</span><span>27</span>                 <span>$sub</span> = <span>$i</span><span>;
</span><span>28</span> <span>            }
</span><span>29</span> <span>        }
</span><span>30</span>         <span>$a</span>[<span>$sub</span>] = <span>$a</span>[<span>$i</span><span>];
</span><span>31</span>         <span>$a</span>[<span>$i</span>] = <span>$min</span><span>;
</span><span>32</span>         <span>//</span><span> echo implode(', ', $a).'<br />';</span>
<span>33</span> <span>    }
</span><span>34</span> <span>}
</span><span>35</span> 
<span>36</span> <span>$arr</span> = <span>array</span>(9, 5, 2, 7, 3<span>);
</span><span>37</span> sortSelection(<span>$arr</span><span>);
</span><span>38</span> <span>echo</span> <span>implode</span>(', ', <span>$arr</span><span>);
</span><span>39</span> 
<span>40</span> <span>//</span><span> 2, 3, 5, 7, 9</span>
<span>41</span> ?>

?

C語言 選擇排序 輸入k個數(shù)字,遞減輸出的selection_sort(array, k)程序

void selection_sort(int array[],int k)
{
int i,j,m,t;
for(i=0;im=i;
for(j=i+1;jif(array[j]m=j; //k記下目前找到的最小值所在的位置
if(m!=i){
t=array[i];
array[i]=array[m];
array[m]=t;
}
}
}
void main(){
int a[10];
for (int i=0;iscanf("%d",&a[i]);
selection_sort(a,10);
printf("排序結(jié)果為:");
for (i=0;iprintf("%d\n",a[i]);
}
?

C語言 選擇排序 輸入k個數(shù)字,遞減輸出的selection_sort(array, k)程序

void selection_sort(int array[],int k)
{
int i,j,m,t;
for(i=0;im=i;
for(j=i+1;jif(array[j]m=j; //k記下目前找到的最小值所在的位置
if(m!=i){
t=array[i];
array[i]=array[m];
array[m]=t;
}
}
}
void main(){
int a[10];
for (int i=0;iscanf("%d",&a[i]);
selection_sort(a,10);
printf("排序結(jié)果為:");
for (i=0;iprintf("%d\n",a[i]);
}
?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explore the underlying principles and algorithm selection of the C++sort function Explore the underlying principles and algorithm selection of the C++sort function Apr 02, 2024 pm 05:36 PM

The bottom layer of the C++sort function uses merge sort, its complexity is O(nlogn), and provides different sorting algorithm choices, including quick sort, heap sort and stable sort.

How to implement drag-and-drop sorting and drag-and-drop operations in uniapp How to implement drag-and-drop sorting and drag-and-drop operations in uniapp Oct 19, 2023 am 09:39 AM

Uniapp is a cross-platform development framework. Its powerful cross-end capabilities allow developers to develop various applications quickly and easily. It is also very simple to implement drag-and-drop sorting and drag-and-drop operations in Uniapp, and it can support drag-and-drop operations of a variety of components and elements. This article will introduce how to use Uniapp to implement drag-and-drop sorting and drag-and-drop operations, and provide specific code examples. The drag-and-drop sorting function is very common in many applications. For example, it can be used to implement drag-and-drop sorting of lists, drag-and-drop sorting of icons, etc. Below we list

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Why doesn't list.sort() return a sorted list in Python? Why doesn't list.sort() return a sorted list in Python? Sep 18, 2023 am 09:29 AM

Example In this example, we first look at the usage of list.sort() before continuing. Here, we have created a list and sorted it in ascending order using sort() method - #CreatingaListmyList=["Jacob","Harry","Mark","Anthony"]#DisplayingtheListprint("List=",myList)#SorttheListsinAscendingOrdermyList .sort(

How to sort a list using List.Sort function in C# How to sort a list using List.Sort function in C# Nov 17, 2023 am 10:58 AM

How to sort a list using the List.Sort function in C# In the C# programming language, we often need to sort the list. The Sort function of the List class is a powerful tool designed for this purpose. This article will introduce how to use the List.Sort function in C# to sort a list, and provide specific code examples to help readers better understand and apply this function. The List.Sort function is a member function of the List class, used to sort elements in the list. This function receives

Use PHP function 'sort' to sort an array in ascending order Use PHP function 'sort' to sort an array in ascending order Jul 25, 2023 am 09:28 AM

Sort an array in ascending order using PHP function "sort" In PHP, you can easily sort an array using built-in functions. Among them, the sort function is one of the most commonly used functions, which can sort an array in ascending order. This article will introduce how to use the sort function and give corresponding code examples. The syntax of the sort function is as follows: sort(array&$array,int$sort_flags=SORT_REGULAR):boo

New numerical sorting function in PHP8.1 New numerical sorting function in PHP8.1 Jul 09, 2023 pm 10:07 PM

New numerical sorting function in PHP8.1 PHP is a widely used open source scripting language commonly used in web development. Not only is it powerful, it also has a rich built-in function library. In the recently released version of PHP 8.1, some interesting features and functions have been added, including numerical sorting functions. These new functions can make it easier for developers to sort numerical arrays, improving development efficiency and code readability. In past PHP versions, we usually used the sort() or rsort() function logarithm

How to use sort sorting function How to use sort sorting function Sep 04, 2023 am 11:11 AM

Usage of sort sorting function: 1. Sort the list. By default, the sort function sorts in ascending order, so the final output results are arranged in order from small to large; 2. Sort the tuples. By default, The sort function sorts by the size of the elements, so the final output results are arranged in order from small to large; 3. Sort the dictionary. Since the dictionary is unordered, the sorted result is still the original dictionary. Use A lambda expression is used as the value of the key parameter to specify the basis for sorting.

See all articles