随机产生十个10-100的整数,用选择排序法按值从小到大顺序排序,我是学VB的。急急。。。我要具体的方法

作者&投稿:迪子 (若有异议请与网页底部的电邮联系)
用选择法对10个整数从小到大排序~

用选择法对10个整数排序,用scanf输入。你用下面这种输入方法就可以了。
# include
int main()
{
int i,j,min,temp,a[10];
printf("enter data:
");
for (i=0;i<10;i++)
{
printf("please enter ten numbers:
");
scanf("%d",&a[i]); //输入10个数
}
printf("
");
printf("The orginal numbers:
");
for (i=0;i<10;i++) //在c语言中,数组的下标从0开始
printf("%5d",a[i]); //输出这10个数
printf("
"); //以下8行是对这10个数排序
for (i=0;i<10;i++)
{min=i;
for(j=i+1;j<10;j++)
if (a[min]>a[j]) min=j;
temp=a[i]; //以下3行将a[i+1]~a[10]中最小值与a[i]对换
a[i]=a[min];
a[min]=temp;
}
printf("
The sorted numbers:
"); //输出已排好序的10个数
for(i=0;i<10;i++)
printf("%5d",a[i]);
printf("
");
return 0;
}

扩展资料:
选择法排序基本思想:
每一趟在n-i+1(i=1,2,…n-1)个记录中选取关键字最小的记录作为有序序列中第i个记录。基于此思想的算法主要有简单选择排序、树型选择排序和堆排序。
简单选择排序的基本思想:第1趟,在待排序记录r[1]~r[n]中选出最小的记录,将它与r交换;第2趟,在待排序记录r[2]~r[n]中选出最小的记录,将它与r[2]交换;以此类推,第i趟在待排序记录r[i]~r[n]中选出最小的记录,将它与r[i]交换,使有序序列不断增长直到全部排序完毕。
以下为简单选择排序的存储状态,其中大括号内为无序区,大括号外为有序序列:
初始序列:{49 27 65 97 76 12 38}
第1趟:12与49交换:12{27 65 97 76 49 38}
第2趟:27不动 :12 27{65 97 76 49 38}
第3趟:65与38交换:12 27 38{97 76 49 65}
第4趟:97与49交换:12 27 38 49{76 97 65}
第5趟:76与65交换:12 27 38 49 65{97 76}
第6趟:97与76交换:12 27 38 49 65 76 97 完成
参考资料:
百度百科- 用选择法对10个整数排序

急什么啊,百度上很多啊,随便搜一下就有了.

Private Sub Command1_Click()
Dim su(1 To 10) As Integer '定义数组
'产生随机数
For i = 1 To 10
su(i) = Rnd * 90 + 10 '产生10-100的数
Print su(i);
Next
Print
'排序
'这样好像是选择排序吧,先选一个最小的数放在前面
For i = 1 To 9
For j = i To 10
If su(i) > su(j) Then
t = su(i)
su(i) = su(j)
su(j) = t
End If
Next
Print su(i);
Next
Print su(i)
End Sub

Private Sub Form_Click()
Dim arr(9) As Integer
For i = 0 To 9
arr(i) = Int(Rnd * 91) + 10
Next i
SelectSort arr
For i = 0 To 9
Print arr(i);
Next i
Print
End Sub

Public Sub SelectSort(ByRef a, Optional ByVal Left, Optional ByVal Right)
'选择排序
'基本思想是:每次选出第i小的记录,放在第i个位置。
'i的起点是Left。当i=Right-1时就排完了。

Dim i As Integer, j As Integer
Dim k As Integer

If IsMissing(Left) Then Left = LBound(a)
If IsMissing(Right) Then Right = UBound(a)

For i = Left To Right - 1
k = i
For j = i + 1 To Right
If a(k) > a(j) Then k = j
Next j
If k <> i Then Swap a(k), a(i)
Next i

End Sub

Public Sub Swap(ByRef a, ByRef b)
'交换

Dim t
t = a: a = b: b = t
End Sub

窗体上加一list1,一command1,代码如下

Private Sub Command1_Click()
Dim arr(10)
Randomize
For I = 0 To 9
arr(I) = Int(Rnd * 90)+10
Next
Selection arr(), 0
For I = 0 To 9
List1.AddItem arr(I)
Next
End Sub

标准模块,里面有常用的7种排序方法,Selection是选择排序

Option Explicit
Global Const ZERO = 0
Global Const ASCENDING_ORDER = 0
Global Const DESCENDING_ORDER = 1

Global gIterations

Sub BubbleSort(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim TEMP
Dim NextElement

NextElement = ZERO
Do While (NextElement < UBound(MyArray))
Index = UBound(MyArray)
Do While (Index > NextElement)
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Index - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = TEMP
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Index - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = TEMP
End If
End If
Index = Index - 1
gIterations = gIterations + 1
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Loop

End Sub

Sub Bucket(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim NextElement
Dim TheBucket

NextElement = LBound(MyArray) + 1
While (NextElement <= UBound(MyArray))
TheBucket = MyArray(NextElement)
Index = NextElement
Do
If Index > LBound(MyArray) Then
If nOrder = ASCENDING_ORDER Then
If TheBucket < MyArray(Index - 1) Then
MyArray(Index) = MyArray(Index - 1)
Index = Index - 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If TheBucket >= MyArray(Index - 1) Then
MyArray(Index) = MyArray(Index - 1)
Index = Index - 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
gIterations = gIterations + 1
Loop
MyArray(Index) = TheBucket
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend

End Sub

Sub Heap(MyArray())
Dim Index
Dim Size
Dim TEMP

Size = UBound(MyArray)

Index = 1
While (Index <= Size)
Call HeapSiftup(MyArray(), Index)
Index = Index + 1
gIterations = gIterations + 1
Wend

Index = Size
While (Index > 0)
TEMP = MyArray(0)
MyArray(0) = MyArray(Index)
MyArray(Index) = TEMP
Call HeapSiftdown(MyArray(), Index - 1)
Index = Index - 1
gIterations = gIterations + 1
Wend

End Sub

Sub HeapSiftdown(MyArray(), M)
Dim Index
Dim Parent
Dim TEMP

Index = 0
Parent = 2 * Index

Do While (Parent <= M)

If (Parent < M And MyArray(Parent) < MyArray(Parent + 1)) Then
Parent = Parent + 1
End If

If MyArray(Index) >= MyArray(Parent) Then
Exit Do
End If

TEMP = MyArray(Index)
MyArray(Index) = MyArray(Parent)
MyArray(Parent) = TEMP

Index = Parent
Parent = 2 * Index

gIterations = gIterations + 1
Loop
End Sub

Sub HeapSiftup(MyArray(), M)
Dim Index
Dim Parent
Dim TEMP

Index = M
Do While (Index > 0)
Parent = Int(Index / 2)

If MyArray(Parent) >= MyArray(Index) Then
Exit Do
End If

TEMP = MyArray(Index)
MyArray(Index) = MyArray(Parent)
MyArray(Parent) = TEMP

Index = Parent
gIterations = gIterations + 1
Loop

End Sub

Sub Insertion(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim TEMP
Dim NextElement

NextElement = LBound(MyArray) + 1
While (NextElement <= UBound(MyArray))
Index = NextElement
Do
If Index > LBound(MyArray) Then
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Index - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = TEMP
Index = Index - 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Index - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = TEMP
Index = Index - 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
gIterations = gIterations + 1
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend

End Sub

Sub QuickSort(MyArray(), L, R)
Dim I, J, X, Y

I = L
J = R
X = MyArray((L + R) / 2)

While (I <= J)
While (MyArray(I) < X And I < R)
I = I + 1
Wend
While (X < MyArray(J) And J > L)
J = J - 1
Wend
If (I <= J) Then
Y = MyArray(I)
MyArray(I) = MyArray(J)
MyArray(J) = Y
I = I + 1
J = J - 1
End If
gIterations = gIterations + 1
Wend

If (L < J) Then Call QuickSort(MyArray(), L, J)
If (I < R) Then Call QuickSort(MyArray(), I, R)

End Sub

Sub Selection(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim Min
Dim NextElement
Dim TEMP

NextElement = 0
While (NextElement < UBound(MyArray))
Min = UBound(MyArray)
Index = Min - 1
While (Index >= NextElement)
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Min) Then
Min = Index
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Min) Then
Min = Index
End If
End If
Index = Index - 1
gIterations = gIterations + 1
Wend
TEMP = MyArray(Min)
MyArray(Min) = MyArray(NextElement)
MyArray(NextElement) = TEMP
NextElement = NextElement + 1
gIterations = gIterations - 1
Wend

End Sub

Sub ShellSort(MyArray(), ByVal nOrder As Integer)
Dim Distance
Dim Size
Dim Index
Dim NextElement
Dim TEMP

Size = UBound(MyArray) - LBound(MyArray) + 1
Distance = 1

While (Distance <= Size)
Distance = 2 * Distance
Wend

Distance = (Distance / 2) - 1

While (Distance > 0)

NextElement = LBound(MyArray) + Distance

While (NextElement <= UBound(MyArray))
Index = NextElement
Do
If Index >= (LBound(MyArray) + Distance) Then
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Index - Distance) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - Distance)
MyArray(Index - Distance) = TEMP
Index = Index - Distance
gIterations = gIterations + 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Index - Distance) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - Distance)
MyArray(Index - Distance) = TEMP
Index = Index - Distance
gIterations = gIterations + 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend
Distance = (Distance - 1) / 2
gIterations = gIterations + 1
Wend

End Sub


随机产生十个10-100的整数,用选择排序法按值从小到大顺序排序,我是学VB...
arr(i) = Int(Rnd * 91) + 10 Next i SelectSort arr For i = 0 To 9 Print arr(i);Next i Print End Sub Public Sub SelectSort(ByRef a, Optional ByVal Left, Optional ByVal Right)'选择排序 '基本思想是:每次选出第i小的记录,放在第i个位置。'i的起点是Left。当i=Right-...

随机生成一个十个数的数列,然后找出其中的相同的数,再随机换成另一个...
我学的是MATLAB,C++格式不是很懂,但给你个算法把,用RAND产生十个0到1的随机数,(假如你要1-10的数,就要构成10*RAND;如果十个数字要求是整数,就要用四舍五入或者就近求整等一些函数使其成为整数)生成之后就是比较了,用WHILE循环语句,从K=1开始到K=9;比较A(K)和A(K+1),不等输...

VB代码,计算机随机产生10个1—100的奇数,并由大到小输出
Private Sub Form_Load()Me.Show Dim a(10) As Integer Print "随机产生的10个1-100的奇数:"Randomize For i = 1 To 10 a(i) = Int(100 * Rnd + 1)If a(i) Mod 2 = 0 Then a(i) = a(i) + 1 Print a(i);Next Print Print "从大到小排列:"For i = 1 To 9 For ...

随机产生10个1~200之间的数,求最小值。
int i,j,a[10];time1 = time(NULL); \/* 获得当前的日历时间 *\/ \/* 以系统时间为参数,为即将生成的伪随机数序列设置起点 *\/ srand(time1);\/* 生成十个伪随机数序列 *\/ printf("The 10 random digits are:\\n");for(i=0; i<10; i++){ a[i]=rand()\/200+1;printf("%-5d",...

随机产生十个1-100之间的整数,存放在一维数组中,编写程序统计其中的奇偶...
public static void main(String args[])throws IOException{ int i=0;int r=0;int odd=0;int even=0;int [] array=new int[10];while(i<10){ r=(int) (Math.random()*100);array[i]=r;i++;if(r%2==0){ even++;} else{ odd++;} } System.out.println("随机出的数据是:...

金泉10-1牌螺旋榨油机榨圈排列顺序是怎样
首先要知道是几级榨,比如说,四级榨,它有四个比较宽内壁中间高两边低,把这四个分开放好,第二步,再把其它榨圈安宽度分别依次分好,然后它的顺序基本就出来了。特别注意比较窄一点榨圈内壁的斜纹是朝向出饼的那一头的,不能装反。希望采纳 ...

随机产生一个10到20(包括10和20)之间的正整数,然后逆序输出,有两个按...
怎么逆序输出?你的要求应该是随机10个正整数,大概在10-20之间,然后再从第10个排到第1个?另外,你想用什么程序语言来编写?大概思路如下:定义一个数组 通过for循环,随机产生数值在10-20之间的正整数,将依次储存到数组中 通过for循环,不过数值从大到小,即步长为-1,将数组中的数依次输出 ...

用VFP编程随机产生10个1~100之间的正整数,并求他们的和
rand()函数生成0-1的随机数, 要生成1-100的,需要处理下 clear s=0 ??'十个随机1-100的正整数为:'for i=1 to 10 n=int(rand()*(100-1+1)+1)??n s=s+n endfor ?'它们的和为:',s

用C语言怎样生成10个1-100之间的随机数
电脑、C语言编译器。1、首先,打开C语言编译器,新建一个初始.cpp文件,例如:test.cpp。2、在test.cpp文件中,输入C语言代码:for (int i = 0; i < 10; i++)printf("%d ", rand() % 100 +1);3、编译器运行test.cpp文件,此时成功通过rand产生了10个1-100内的整数。

计数时为什么要满10进1?
我们平时常用的都是10进制,满10进1,也就是当低位的值够10之后,就在高位上加1,本位变成0。2进制就是同理,计数时满2进1,当低位满2之后,就在高位+1,低位变成0。具体,以10进制和2进制的对比来看:十进制---二进制 0 --- 0 1 --- 1 2 --- 10 低位满2,向高位进1,低位归0...

易门县13410058756: 随机产生10个介于0到100之间的整数,分别使用选择法和冒泡法对其进行升序排序 -
泊羽哌库:[答案] 选择法: Private Sub Form_Click() Dim i%,j%,a%(1 To 10) Randomize For i = 1 To 10 a(i) = Int(101 * Rnd) Next i For i = 1 To 9 k = i For j = i + 1 To 10 If a(j) Next j If k i Then t = a(i):a(i) = a(k):a(k) = t Next i For i = 1 To 10 Print a(i); Next End Sub 冒泡法:...

易门县13410058756: 利用随机函数产生10个100以内的整数,并从小到大的顺序排序并输出. -
泊羽哌库: #include <iostream> using namespace std;int a[10]; //测试数组void print() //输出数组元素 {for(int i=0;i<10;i++)cout<<a[i]<<' ';cout<<endl; }void get() //生成0到100的随机数 {for(int i=0;i<10;i++)a[i]=rand()%101;cout<<"生成随机数...

易门县13410058756: 数组的反序输出:随机产生10个100以内的整数,分别按其正序和反序输出 -
泊羽哌库: Java 8import java.util.Random; public class Test {public static void main(String[] args) {//随机产生10个100以内的整数Random r=new Random();//正序r.ints(10,0,100).sorted().mapToObj(Integer::toString).reduce((x,y)-> x+", "+y)....

易门县13410058756: 随机产生十个10 - 100的整数,用选择排序法按值从小到大顺序排序,我是学VB的.急急... -
泊羽哌库: 急什么啊,百度上很多啊百,随便搜一下就有了度.Private Sub Command1_Click() Dim su(1 To 10) As Integer '定义数回组 '产生随机数 For i = 1 To 10su(i) = Rnd * 90 + 10 '产生10-100的数Print su(i); Next Print '排序 '这样好像是选择排序吧,...

易门县13410058756: 朋友们 帮帮忙 用JAVA随机生成10个100以内的整数并按从小到大的顺序排列 感激不尽 -
泊羽哌库: import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet;/**/*** @ClassName: Random* @Description: TODO* @author :* @date May 24, 2012 9:30:49 AM**/ public class Random { /*** @Title: main* @Description: ...

易门县13410058756: 随机产生一个10到100的整数在text上并判断它的奇偶性的VB语言程序? -
泊羽哌库: 随机产生的值到十的整数,这个的话也是可以的,例如12,24这个都可以.

易门县13410058756: 用随机函数产生10个10到100之间的随机整数存放到一维数组a中,并求a中的最小值及最小值 -
泊羽哌库: #include void main() { int a[100],i; for(i=0;i

易门县13410058756: 随机产生10个10到100之间的整数,并将其按从大到小的顺序显示在窗体上(注:用冒泡方法排序,每行输出4个 -
泊羽哌库: Dim a(1 To 10) As Integer Print "未排序前" For i = 1 To 10 Randomize a(i) = Int(Rnd * 91+10) Next i For k = 1 To 10 Print a(k); " "; Next k For j = 2 To 10 For i = 10 To j Step -1 If a(i) < a(i - 1) Then t = a(i): a(i) = a(i - 1): a(i - 1) = t End If Next i ...

易门县13410058756: 用VB写出产生10个1到100的随机整数 -
泊羽哌库: dim a(10) as integer randomize for i=1 to 10a(i)=int(100*rnd)+1 next i

易门县13410058756: 随机生成10个100内的整数,使用选择排序法从大到小排序随机生成10个100内的整数,使用选择排序法从大到小排序用VB编程~谢谢回答! -
泊羽哌库:[答案] Private Sub Form_Click() Dim a(1 To 10) As Integer Randomize tiemr Print "原数组是:" For i = 1 To 10 a(i) = Int(Rnd * 100 + 1) Print a(i); " " Next i Print Print "排序后的数组是:" For i = 1 To 9 For j = i + 1 To 10 If a(i)解析看不懂?免费查看同...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网