本文目錄一覽:
Java的線性查找,二分查找,冒泡排序,插入排序,快速排序的源代碼
C++的,只要把,函數名改下,輸出語句改下,就可以了。希望對你有幫助
void Sort :: SelectionSort(int a[],int n)
{
bool sorted=false;
cout”中間過程為:”endl;
for(int size=n;!sorted size1;size–){
int pos = 0;
sorted = true;
for(int i=1;isize;i++)
if(a[pos]=a[i])pos=i;
else sorted=false;
Swap(a[pos],a[size-1]);
for(int j=0;j!=n;j++){//顯示中間過程
couta[j]” “;
}
coutendl;
}
cout”選擇排序結果為:”endl;
for(int i=0;i!=n;i++){
couta[i]” “;
}
coutendl;
}
/*
冒泡排序
*/
bool Sort :: Bubble(int a[],int m,int n)
{
bool swapped=false;
for(int i=0;im-1;i++){
if(a[i]a[i+1]){
Swap(a[i],a[i+1]);
swapped=true;
for(int j=0;j!=n;j++){//顯示中間過程
couta[j]” “;
}
coutendl;
}
}
return swapped;
}
void Sort :: BubbleSort(int a[],int n)
{
cout”中間過程為:”endl;
for(int i=n;i1 Bubble(a,i,n);i–);
coutendl;
cout”冒泡排序結果為:”endl;
for(i=0;i!=n;i++){
couta[i]” “;
}
coutendl;
}
/*
插入排序
*/
void Sort :: InsertionSort(int a[],int n)
{
cout”中間過程為:”endl;
for (int i=1;in;i++){
int t=a[i];
int j;
for (j=i-1;j=0 ta[j];j–){
a[j+1]=a[j];
}
a[j+1]=t;
for(int k=0;k!=n;k++){//顯示中間過程
couta[k]” “;
}
coutendl;
}
cout”插入排序結果為:”endl;
for(i=0;i!=n;i++){
couta[i]” “;
}
coutendl;
}
/*
基數排序
*/
void Sort :: RadixSort(int a[],int n)
{
int d=1;
int m=10;
for (int i=0;in;i++){
while(a[i]=m){
m*=10;
++d;
}
}
int *t=new int[n];
int *count=new int [10];
int radix=1,k;
for(i=1;i=d;i++){
for(int j=0;j10;j++){
count[j]=0;//每次分配前清空計數器
}
for(j=0;jn;j++){
k=(a[j]/radix)%10;//統計每個桶中的記錄數
count[k]++;
}
cout”分桶顯示:”endl;
for(j=0;j10;j++){//顯示中間xiangxi過程
if(count[j]!=0){
coutj”: “;
for(int l=0;ln;l++){
if ((a[l]/radix)%10==j)
couta[l]” “;
}
coutendl;
}
}
coutendl;
for(j=1;j10;j++){
count[j]=count[j-1]+count[j];
}
for(j=n-1;j=0;j–){
k=(a[j]/radix)%10;
count[k]–;
t[count[k]]=a[j];
}
for(j = 0;j n;j++) {
a[j]=t[j];
}
radix=radix*10;
cout”按桶依次排序排序:”endl;
for(j=0;j!=n;j++){//顯示中間過程
couta[j]” “;
}
coutendl;
}
delete[] t;
delete[] count;
cout”基數排序結果為:”endl;
for(i=0;i!=n;i++){
couta[i]” “;
}
coutendl;
}
JAVA中怎麼實現查詢 代碼
try{Connection con;
Statement stmt;
ResultSet rs;
int temp;
Class.forName(“com.mysql.jdbc.Driver”);
con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/java”,”root”,””);//以上是數據庫連接,不同的數據管理器有 //不同的驅動和鏈接方式,以上是mysql的連接
stmt=con.createStatement();
rs=stmt.executeQuery(“select * from student”);//執行查詢語句,結果賦值給結果集rs
//結果集是結果於字段編號的映射,每一個字
//段都有一個編號,最小為1,也就是第一個字段
while(rs.next()){
String names=rs.getString(“name”);//查詢結果轉換成字符串。
System.out.println(names);
}rs.close();
}catch(Exception e){
e.printStackTrace();
}
有關JAVA簡單線性查找的問題,源碼如下:
那個key是linearSearch方法的形參 是調用該方法的時候需要傳入的參數
比如 linearSearch(array, 6);
這裡 key就是6
你可以加個main函數試下
public static void main(String[] args) {
int array2[] = {1,2,3,4,5};
LinearSearch search = new LinearSearch();
int result = search.linearSearch(array2, 6 );
System.out.println(result);
}
不過我也看了看 這個方法跟array2沒多大關係 就只取了array2的長度
再在array裡邊去找key,意義不大 估計寫錯了~
百科裏邊這個線性查找才是正確的
int display(int array[],int b,int conter)
在數組array里查找conter 從array[0]找到array[b]
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/302051.html