C# 冒泡排序,大到小 新手,请教:面试题目, C#写个冒泡排序,要求从大到小输出. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public void bubblessort(int[] data,int n) { int i, j, tag, temp; for(i = 0,tag =1;i < n - 1 && tag == 1;i++) { tag = 0; for (j = 0; j < n - i - 1;j++) { if(data[j] > data[j+1]) { temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; tag = 1; } } } } int t;bool b=false;for(int i=0;i<100 && !b;i++){ b=true; for(int j=i+1;j<100;j++) { if(a[j]<a[i]) { t=a[i]; a[i]=a[j]; a[j]=t; b=false; } }} using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2{ class Program { static void Main(string[] args) { int[] myArray = new int[] { 10, 8, 3, 5, 6, 7, 4, 6, 9 }; for (int k=0; k < 9; k++) Console.Write("{0},",myArray[k]); Console.WriteLine(); // 取长度最长的词组 -- 冒泡法 for (int j = 1; j < myArray.Length; j++) { for (int i = 0; i < myArray.Length - 1; i++) { // 如果 myArray[i] < myArray[i+1] ,则 myArray[i] 下沉一位 if (myArray[i] < myArray[i + 1]) { int temp = myArray[i]; myArray[i] = myArray[i + 1]; myArray[i + 1] = temp; } } } for (int k=0; k < 9; k++) Console.Write("{0},", myArray[k]); Console.ReadKey(); } }}//结帖 int[] array = new int;int temp = 0;for(int i = 0;i < array.length - 1;i++){ for(int j = i + 1;j < array.length;j++) { if(array[j] < array[i]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } }} public void BubbleSort(int[] R) { int i,j,temp; //交换标志 bool exchange; for(i=0; i<R.Length; i++) //最多做R.Length-1趟排序 { exchange=false; //本趟排序开始前,交换标志应为假 for(j=R.Length-2; j>=i; j--) { if(R[j+1]<R[j]) //交换条件 { temp=R[j+1]; R[j+1]=R[j]; R[j]=temp; exchange=true; //发生了交换,故将交换标志置为真 } } if(!exchange) //本趟排序未发生交换,提前终止算法 { break; } } } using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication3{ class Program { static void Main(string[] args) { int[] a = { 3, 4, 7, 10, 5, 9 }; int[] b = BubbleSort(a); for (int i = 0; i < b.Length; i++) { Console.Write(b[i].ToString() + " "); } Console.ReadLine(); } public static int[] BubbleSort(int[] list) { int i, temp; for (int j = 0; j < list.Length; j++) { for (i = list.Length - 1; i > j; i--) { if (list[j] < list[i]) { temp = list[j]; list[j] = list[i]; list[i] = temp; } } } return list; } }} 在解释冒泡排序算法之前,先来介绍把10个数(放在数组A中)中最大的那个数放在最后位置上的一种算法。算法描述如下: (1)从数组A[1]到A[10],把相临的两个数两两进行比较。即A[1]和A[2]比较,比较完后A[2]再与A[3]比较,……最后是A[9]和A[10]比较。 (2)在每次进行比较的过程中,如果前一个数比后一个数大,则对调两个数,也就是说把较大的数调到后面,较小的调到前面。比如在第一次的比较中,如果A[1]比A[2]大则A[1]和A[2]的值就互换。下图用6个数据来说明以上的算法。 假设6个数据是:A[]=5 7 4 3 8 6 A[1] A[2] A[3] A[4] A[5] A[6] 5 7 4 3 8 6 第一次,A[1]=5和A[2]=7比较,7>5,不进行对调。 5 7 4 3 8 6 第二次,A[2]=7和A[3]=4比较,4<7,进行对调, 那么第二次比较完后的数据是5 4 7 3 8 6 5 4 7 3 8 6 第三次,A[3]=7和A[4]=3比较,3<7,进行对调, 那么第三次比较完后的数据是5 4 3 7 8 6 5 4 3 7 8 6 第四次,A[4]=7和A[5]=8比较,8>7,不进行对调。 5 4 3 7 8 6 第五次,A[6]=6和A[5]=8比较,6<8,进行对调, 那么第五次也就是最后一次的结果是 5 4 3 7 6 8 由上例可以看出,对于6个数,排好一个数(最大的数)需要进行5次比较,可以推断出,对于N个数,一躺需要 N-1次比较操作 int[] array = new int[];int temp = 0;for(int i = 1;i < array.length ;i++){ for(int j = i ;j < array.length-i;j++) { if(array[j] < array[i]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } }} c# winForm usercontrol之间传值 请问这个from子句语法错误是怎么回事 c#怎么打开任务栏里的QQ DataRow类型向DataGridViewRow转换 请教socket基于TCP协议时心跳的问题,困扰了我很久. 关于矩形和线段相交的问题 c#做web容器控件如何做? 关于HttpContext问题,高手过来看看! c# 有关word application和excel application同时使用问题 由于系统缓冲区或队列已满,不能执行套接字上的操作 ??? 学.net有必要学习Windows API吗? 为什么要使用事件?
{
int i, j, tag, temp;
for(i = 0,tag =1;i < n - 1 && tag == 1;i++)
{
tag = 0;
for (j = 0; j < n - i - 1;j++)
{
if(data[j] > data[j+1])
{
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
tag = 1;
}
}
}
}
int t;
bool b=false;
for(int i=0;i<100 && !b;i++)
{
b=true;
for(int j=i+1;j<100;j++)
{
if(a[j]<a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
b=false;
}
}
}
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] myArray = new int[] { 10, 8, 3, 5, 6, 7, 4, 6, 9 }; for (int k=0; k < 9; k++)
Console.Write("{0},",myArray[k]);
Console.WriteLine();
// 取长度最长的词组 -- 冒泡法
for (int j = 1; j < myArray.Length; j++)
{
for (int i = 0; i < myArray.Length - 1; i++)
{
// 如果 myArray[i] < myArray[i+1] ,则 myArray[i] 下沉一位
if (myArray[i] < myArray[i + 1])
{
int temp = myArray[i];
myArray[i] = myArray[i + 1];
myArray[i + 1] = temp;
}
}
} for (int k=0; k < 9; k++)
Console.Write("{0},", myArray[k]);
Console.ReadKey();
}
}
}
//结帖
int[] array = new int;
int temp = 0;
for(int i = 0;i < array.length - 1;i++)
{
for(int j = i + 1;j < array.length;j++)
{
if(array[j] < array[i])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
{
int i,j,temp; //交换标志 bool exchange; for(i=0; i<R.Length; i++) //最多做R.Length-1趟排序
{
exchange=false; //本趟排序开始前,交换标志应为假 for(j=R.Length-2; j>=i; j--)
{
if(R[j+1]<R[j]) //交换条件
{
temp=R[j+1];
R[j+1]=R[j];
R[j]=temp; exchange=true; //发生了交换,故将交换标志置为真
}
} if(!exchange) //本趟排序未发生交换,提前终止算法
{
break;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{ int[] a = { 3, 4, 7, 10, 5, 9 };
int[] b = BubbleSort(a);
for (int i = 0; i < b.Length; i++)
{
Console.Write(b[i].ToString() + " "); }
Console.ReadLine();
} public static int[] BubbleSort(int[] list)
{
int i, temp;
for (int j = 0; j < list.Length; j++)
{
for (i = list.Length - 1; i > j; i--)
{
if (list[j] < list[i])
{
temp = list[j];
list[j] = list[i];
list[i] = temp;
}
}
} return list;
}
}
}
int temp = 0;
for(int i = 1;i < array.length ;i++)
{
for(int j = i ;j < array.length-i;j++)
{
if(array[j] < array[i])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}