Private Sub Command4_Click()
Dim t As clerk, i%, j%
For i = 0 To n - 1
For j = i To n - 2
If a(i)vc > a(j + 1)vc Then
t = a(i): a(i) = a(j + 1): a(j + 1) = t
End If
Next j
Next i
Picture2Cls
Picture2Print "学号 姓名 VC VB"
Picture2Print "---------------------------------------------"
For i = 0 To n - 1
Picture2Print a(i)number, a(i)name, a(i)vc, a(i)vb
Next i
End Sub
扩展资料
vb数组排序思路:
1、冒泡排序法:
位置相邻两数进行两两比较,在比较时如果发现前面的数比后面的数大,则进行交换,都比较完一轮后,把最大一个数放到最后,如此进行下去即可完成冒泡排序。
2、比较交换法
假设第一个数最小,然后第一个数依次与后面的每个数都进行比较, 若比较时发现后面的数比第一个数小, 则两数位置进行交换, 全部都比较完算一轮,每一轮比较完后,第一个数是最小的数,如此进行即可完成比较排序。
3、选择排序
假设第一个数最小,接着记下最小数所在的位置,然后将最小数依次与后面的每一个数都进行比较,若比较时发现后面的数比最小的数还小,则修改最小数所在位置,全部都比较完算一轮。
每一轮比较完后,最小数所在的位置是否跟假设的是同一个位置,若不是,则最小数与第一个数进行交换位置,如此进行即可完成选择排序。
#include<stdioh>
#include<stdlibh>
#define shengxu 0
void RandonArray(int a[],int n)
{
int i;
srand(time(NULL));
for(i=0;i<n;i++)
{
a[i]=rand();
}
}
void SortArray(int a[],int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(shengxu)
{
if(a[j]>a[j+1])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
else
{
if(a[j]<a[j+1])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
}
}
}
void OutputArray(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d,",a[i]);
}
}
int main()
{
int i,n,a;
char msg[]={-56,-25,-71,-5,-78,-55,-60,-55,-93,-84,-57,-21,-72,-8,-50,
-46,50,48,48,-78,-58,-72,-69,-93,-95,0};
printf("%s\n\n",msg);
printf("请输入要随机产生的数组的元素个数:");
scanf("%d",&n);
a=(int)calloc(n,sizeof(int));
RandonArray(a,n);
printf("%s排序前数组各元素的值是:\n",shengxu "升序" : "降序");
OutputArray(a,n);
SortArray(a,n);
printf("\n%s排序后数组各元素的值是:\n",shengxu "升序" : "降序");
OutputArray(a,n);
free(a);
printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}
//排序是一种极其重要的算法。常见的排序有冒泡排序(O(n^2))、选择排序(O(n^2))、插入排序(O(n^2))、快速排序(O(nlog(n)))、堆排序(O(nlog(n)))、归并排序(O(nlog(n)))、桶排序(O(n))等
//1冒泡排序(程序摘自:>
#include<stdioh>
#include<malloch>
int main()
{
int num;
printf("请输入元素的个数:");
scanf("%d",&num);
char p=(char)malloc(sizeof(char)num);
for(int i=0;i<num;i++)
{
printf("请输入第%d个元素",i);
fflush(stdin);
scanf("%c",p+i);
}
for(int i=0;i<num;i++)
{
printf("%c ",(p+i));
}
FILE fp;
if(!(fp=fopen("atxt","wb+")))
{
printf("打开文件失败");
return 1;
}
for(int i=0;i<num;i++)
{
fputc((p+i),fp);
}
return 0;
}
在ccs上已经调试了,还有不懂的话留邮箱!
初步看,你的void MySwap(int a,int b)函数有问题,形参既不是指针也不是引用,两个普通的int变量接收的只是拷贝,在函数中交换是白做工作!改成void MySwap(int &a,int &b)就可以了。其他有没有问题未看!第二问我也不知道。
代码如下:~~~~~~~~~~~~~~~~~~~~~~~~
public class SortAll {
/
冒泡排序,选择排序,插入排序,希尔(Shell)排序 Java的实现
20100425
@author panguiming
/
public static void main(String[] args) {
int[] i = { 1, 5, 6, 12, 4, 9, 3, 23, 39, 403, 596, 87 };
Systemoutprintln("----冒泡排序的结果:");
maoPao(i);
Systemoutprintln();
Systemoutprintln("----选择排序的结果:");
xuanZe(i);
Systemoutprintln();
Systemoutprintln("----插入排序的结果:");
chaRu(i);
Systemoutprintln();
Systemoutprintln("----希尔(Shell)排序的结果:");
shell(i);
}
// 冒泡排序
public static void maoPao(int[] x) {
for (int i = 0; i < xlength; i++) {
for (int j = i + 1; j < xlength; j++) {
if (x[i] > x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
for (int i : x) {
Systemoutprint(i + " ");
}
}
// 选择排序
public static void xuanZe(int[] x) {
for (int i = 0; i < xlength; i++) {
int lowerIndex = i;
// 找出最小的一个索引
for (int j = i + 1; j < xlength; j++) {
if (x[j] < x[lowerIndex]) {
lowerIndex = j;
}
}
// 交换
int temp = x[i];
x[i] = x[lowerIndex];
x[lowerIndex] = temp;
}
for (int i : x) {
Systemoutprint(i + " ");
}
}
// 插入排序
public static void chaRu(int[] x) {
for (int i = 1; i < xlength; i++) {// i从一开始,因为第一个数已经是排好序的啦
for (int j = i; j > 0; j--) {
if (x[j] < x[j - 1]) {
int temp = x[j];
x[j] = x[j - 1];
x[j - 1] = temp;
}
}
}
for (int i : x) {
Systemoutprint(i + " ");
}
}
// 希尔排序
public static void shell(int[] x) {
// 分组
for (int increment = xlength / 2; increment > 0; increment /= 2) {
// 每个组内排序
for (int i = increment; i < xlength; i++) {
int temp = x[i];
int j = 0;
for (j = i; j >= increment; j -= increment) {
if (temp < x[j - increment]) {
x[j] = x[j - increment];
} else {
break;
}
}
x[j] = temp;
}
}
for (int i : x) {
Systemoutprint(i + " ");
}
}
}
运行结果如下:~~~~~~~~~~~~~~~~~~~~~~~~~
----冒泡排序的结果:
1 3 4 5 6 9 12 23 39 87 403 596
----选择排序的结果:
1 3 4 5 6 9 12 23 39 87 403 596
----插入排序的结果:
1 3 4 5 6 9 12 23 39 87 403 596
----希尔(Shell)排序的结果:
1 3 4 5 6 9 12 23 39 87 403 596
ASSUME CS:CC, DS:QW
;------------------------------
QW SEGMENT
BUF DB 36, 78, -1, 0, -54, 33, 53, -40, 78, 100
LLL EQU $ - BUF
QW ENDS
;------------------------------
CC SEGMENT
START:
MOV AX, QW
MOV DS, AX
;-------------------------
MOV CX, LLL
DEC CX
LP1:
MOV DX, CX
LEA BX, BUF ;起始地址
LP2:
MOV AL, [BX]
CMP AL, [BX + 1]
JL NEXT
XCHG AL, [BX + 1]
MOV [BX], AL
NEXT:
INC BX
DEC DX
CMP DX, 0
JNZ LP2
LOOP LP1
;-------------------------
EXIT:
MOV AH, 4CH
INT 21H
CC ENDS
END START
;------------------------------
#include
"stdioh"
void
reverse(int
a[],int
pos,int
n)//把数组a中从下标pos开始的n个数逆置
{
int
p=a+pos;
int
q=a+pos+n-1;
while(p<q)
{
int
temp=p;
p=q;;
q=temp;
p++;q--;
}
}
void
main()
{
int
a[10]={1,2,3,4,5,6,7,8,9,10};
int
pos,n;
int
i;
scanf("%d%d",&pos,&n);
reverse(a,pos,n);
for(i=0;i<10;i++)
printf("%d
",a[i]);
}
以上就是关于VB中如何给指定的数组排序全部的内容,包括:VB中如何给指定的数组排序、C语言作业:编写程序实现整型数组的排序、c++中数组如何排序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)