求大神幫解一到c++題 排序查詢 線上等挺急的 希望直接把原始碼貼上來

Jul23

求大神幫解一到c++題 排序查詢 線上等挺急的 希望直接把原始碼貼上來  以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

求大神幫解一到c++題 排序查詢 線上等挺急的 希望直接把原始碼貼上來

求大神幫解一到c++題 排序查詢 線上等挺急的 希望直接把原始碼貼上來

#include<iostream>using namespace std;void print_array(int *a,int n){ for(int i=0;i<n;i++){  cout<<a[i]<<" "; } cout<<endl;}void select_sort(int *a,int n){ for(int i=0; i<n-1; i++) {  int index=i;  for(int j=i+1; j<n; j++)  {   if(a[j]>a[index])尋找無序區內的最大值    index=j;  }  if(index!=i)             把找到的最大值放到無序區的最前面  {   int tmp=a[index];   a[index]=a[i];   a[i]=tmp;  } }}void binary_search(int value,int *a,int n){ if ( a == NULL || n == 0 ) cout<<"Not Found!"<<endl;    int low = 0;      int high = n-1;      int mid = 0;    bool flag=false;       while ( low <= high )      {  mid = (low + high )/2;                if (a[mid] < value)    high = mid - 1;       else if (a[mid] > value)    low = mid + 1;         else{       cout<<"Found!The Index is "<<mid<<endl;       flag=true;    break;    }    } if(!flag) cout<<"Not Found!"<<endl;    }int main(){ int a[1000],n,i=0; cout<<"Please input array a:(end with -1)"<<endl; do{  cin>>a[i++]; }while(a[i-1]!= -1); n=--i; cout<<"The Original Array is:"<<endl; print_array(a,n); select_sort(a,n); cout<<"The Sorted Array is:"<<endl; print_array(a,n); int k; cout<<"Please input the number you want to search: "; cin>>k; cout<<"The result is "; binary_search(k,a,n);}
此外,該程式只能找出與k相同的一個數的下標,而不能找出所有與k相同的數的下標。如要找出,你只需在二分查詢里加入條件,掃描a[mid]值的前後是否與a[mid]相同即可輸出

我補充到下面:

在binary_search的else裡改成下面的程式碼即可輸出所有下標:

else{       cout<<"Found!The Index is "<<mid<<" ";       int tmp=mid+1;       mid--;       while(a[mid]==value){        cout<<mid<<" ";         mid--;    }    while(a[tmp]==value){     cout<<tmp<<" ";     tmp++;    }    cout<<endl;       flag=true;    break;    }

Java的線性查詢,二分查詢,氣泡排序,插入排序,快速排序的原始碼

C++的,只要把,函式名改下,輸出語句改下,就可以了。希望對你有幫助
void Sort :: SelectionSort(int a[],int n)
{
bool sorted=false;
cout<<"中間過程為:"<<endl;
for(int size=n;!sorted && size>1;size--){
int pos = 0;
sorted = true;
for(int i=1;i<size;i++)
if(a[pos]<=a[i])pos=i;
else sorted=false;
Swap(a[pos],a[size-1]);
for(int j=0;j!=n;j++){顯示中間過程
cout<<a[j]<<" ";
}
cout<<endl;
}
cout<<"選擇排序結果為:"<<endl;
for(int i=0;i!=n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
/*
氣泡排序
*/
bool Sort :: Bubble(int a[],int m,int n)
{
bool swapped=false;

for(int i=0;i<m-1;i++){
if(a[i]>a[i+1]){
Swap(a[i],a[i+1]);
swapped=true;
for(int j=0;j!=n;j++){顯示中間過程
cout<<a[j]<<" ";
}
cout<<endl;
}
}
return swapped;
}
void Sort :: BubbleSort(int a[],int n)
{
cout<<"中間過程為:"<<endl;
for(int i=n;i>1 && Bubble(a,i,n);i--);
cout<<endl;
cout<<"氣泡排序結果為:"<<endl;
for(i=0;i!=n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
/*
插入排序
*/
void Sort :: InsertionSort(int a[],int n)
{
cout<<"中間過程為:"<<endl;
for (int i=1;i<n;i++){
int t=a[i];
int j;
for (j=i-1;j>=0 && t<a[j];j--){
a[j+1]=a[j];
}
a[j+1]=t;
for(int k=0;k!=n;k++){顯示中間過程
cout<<a[k]<<" ";
}
cout<<endl;
}
cout<<"插入排序結果為:"<<endl;
for(i=0;i!=n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
/*
基數排序
*/
void Sort :: RadixSort(int a[],int n)
{
int d=1;
int m=10;
for (int i=0;i<n;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;j<10;j++){
count[j]=0;每次分配前清空計數器
}
for(j=0;j<n;j++){
k=(a[j]/radix)%10;統計每個桶中的記錄數
count[k]++;
}
cout<<"分桶顯示:"<<endl;
for(j=0;j<10;j++){顯示中間xiangxi過程
if(count[j]!=0){
cout<<j<<": ";
for(int l=0;l<n;l++){
if ((a[l]/radix)%10==j)
cout<<a[l]<<" ";
}
cout<<endl;
}
}
cout<<endl;
for(j=1;j<10;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++){顯示中間過程
cout<<a[j]<<" ";
}
cout<<endl;
}
delete[] t;
delete[] count;
cout<<"基數排序結果為:"<<endl;
for(i=0;i!=n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}

求C++五個數字排序的原始碼謝謝了,大神幫忙啊

#include <iostream.h> void main() { int a[5]; for(int i=0; i<5; i++) cin>>a[i]; for(i=0; i<4; i++) for(int j=i+1; j<5; j++) if(a[i] < a[j]) {int temp = a[i]; a[i] = a[j]; a[j] = temp; } for(i=0; i<5; i++) cout<<a[i]<<" "; }

求visual c++氣泡排序的程式原始碼!

fds.cpp : 定義控制檯應用程式的入口點。

#include "stdafx.h"
void main()
{
int a[5],b,c;
printf("請輸入您要比較的5個數:\n");
for(int i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(c=0;c<5-1;c++)
{
for(b=0;b<5-c-1;b++)
{
if(a[b]<a[b+1])
{
int t;
t=a[b];
a[b]=a[b+1];
a[b+1]=t;
}
}
}
for(int k=0;k<5;k++)
{
printf("%d ",a[k]);
}
printf("\n\n");
}
比較簡單的用冒泡法實現的5個數比大小.最後從大到小排列輸出.

求C++五個數字排序的原始碼

#include <iostream.h>
void main()
{
int a[5];
for(int i=0; i<5; i++)
cin>>a[i];
for(i=0; i<4; i++)
for(int j=i+1; j<5; j++)
if(a[i] < a[j])
{int temp = a[i];<br/> a[i] = a[j];<br/> a[j] = temp;<br/> }
for(i=0; i<5; i++)
cout<<a[i]<<" ";
}

c++中用選擇法排序10個數,求原始碼

#include"iostream"
#include"string"
#include"iomanip"
using namespace std;
template<typename T,int size>class orderedlist orderedlist list class template
{
private:
T slist[size];
int maxsize;
int last;
public:
orderedlist();
~orderedlist();
int Length()const; calculate the length of the orderedlist list
int Find(T &x)const; find x's position in the orderedlist list
bool IsIn(T &x); judge whether x is in the orderedlist list
bool Insert(T &x,int); add x to int in the orderedlist list
bool Remove(T &x); delete x from the orderedlist list
int Next(T &x); the subscript of x's next member
int Prior(T &x); the subscript of x's prior member
bool IsEmpty(); judge whether the orderedlist is empty or not
bool IsFull(); judge whether the orderedlist is full or not
T Get(int); get the int member of the orderedlist list
T &operator[](int); overload the operator []
int BinarySearch(T &x,const int,const int); binary search x
void Print();
void InsertSort(); ascendind insert sorting algorithm
void BubbleSort(); bubble sort algorithm
void SelectSort(); straight select sort
};
template<typename T,int size>orderedlist<T,size>::orderedlist()
{
last=-1;
maxsize=size;
}
template<typename T,int size>orderedlist<T,size>::~orderedlist()
{
}
template<typename T,int size>int orderedlist<T,size>::Length()const const means the data visited can not be modified
{
return last+1;
}
template<typename T,int size>int orderedlist<T,size>::Find(T &x)const
{
int i=0;
while(i<=last&&slist[i]!=x) sequential search the x
{
i++;
}
if(i>size)
{
return -1; if x is not found,return -1
}
return i; if x is found,return x's subscript
}
template<typename T,int size>bool orderedlist<T,size>::IsIn(T &x)
{
int i=0;
bool found=0;
while(i<=last&&!found)
{
if(slist[i]!=x) x is not in the orderedlist list
{
i++;
}
else
{
found=1; x is in the orderedlist list
}
}
return found;
}
template<typename T,int size>bool orderedlist<T,size>::Insert(T &x,int i)
{
int j;
if(i<0||i>last+1||last==maxsize-1) x's position is unreasonable
{
return false; fail to insert the x
}
else
{
last++;
for(j=last;j>i;j--)
{
slist[j]=slist[j-1]; move back from the last position of the orderedlist list
}
slist[i]=x; insert x
return true; suess to insert x
}
}
template<typename T,int size>bool orderedlist<T,size>::Remove(T &x)
{
int i,j;
i=Find(x);
if(IsIn(x))
{
last--;
for(j=i;j<=last;j++)
{
slist[j]=slist[j+1]; move forword from x's position
}
return true; delete x suessfully
}
return false; fail to delete x
}
template<typename T,int size>int orderedlist<T,size>::Next(T &x)
{
int i;
i=Find(x);
if(i>=0&&i<last)
{
return i+1;
}
return -1; x is not in the orderedlist list or x's subscript is the last position
}
template<typename T,int size>int orderedlist<T,size>::Prior(T &x)
{
int i;
i=Find(x);
if(i>0&&i<=last)
{
return i-1;
}
return -1; x is not in the orderedlist list or x's subscript is the first position
}
template<typename T,int size>bool orderedlist<T,size>::IsEmpty()
{
return last==-1;
}
template<typename T,int size>bool orderedlist<T,size>::IsFull()
{
return last==maxsize-1;
}
template<typename T,int size>T orderedlist<T,size>::Get(int i)
{
return i<0||i>last?NULL:slist[i];
}
template<typename T,int size>T & orderedlist<T,size>::operator[](int i)
{
if(i<0||i>last+1||i>=maxsize)
{
cout<<"Subscript is out of the boundary!"<<endl;
exit(1);
}
if(i>last)
{
last++;
}
return slist[i];
}
template<typename T,int size>int orderedlist<T,size>::BinarySearch(T &x,const int low,const int high)
{
int mid=-1;
if(low<=high) ascending binary search x
{
mid=(low+high)/2;
if(x<slist[mid]) call overload operator< ,equal to x.key<slist[mid].key
{
mid=BinarySearch(x,low,mid-1); high=mid-1;
}
else if(slist[mid]<x) call overload operator< ,equal to slist[mid].key<x.key
{
mid=BinarySearch(x,mid+1,high); low=mid+1;
}
}
return mid;
}
template<typename T,int size>void orderedlist<T,size>::Print()
{
int i;
for(i=0;i<=last;i++)
{
slist[i].Show(); show Element slist[i]'s member data--key
if(i%5==4)
{
cout<<endl;
}
}
cout<<endl;
}
template<typename T,int size>void orderedlist<T,size>::InsertSort() ascendind insert sorting algorithm
{
T temp;
int i,t;
for(i=1;i<=last;i++) the first member data is sorted,starting from the second one
{
temp=slist[i];
t=i;
while(t>0&&temp<slist[t-1]) if the data is smaller than the former one
{
slist[t]=slist[t-1]; change the position
t--;
}
slist[t]=temp;
}
}
template<typename T,int size>void orderedlist<T,size>::BubbleSort() bubble sort algorithm
{
int i,j;
T temp;
bool noswap; swap sign
for(i=0;i<last;i++)
{
noswap=true; no swap
for(j=last;j>i;j--)
{
if(slist[j]<slist[j-1]) the member data is smaller than the prior
{
temp=slist[j];
slist[j]=slist[j-1];
slist[j-1]=temp;
noswap=false; swap
}
}
if(noswap)
{
break;
}
}
}
template<typename T,int size>void orderedlist<T,size>::SelectSort() straight select sort algorithm
{
int i,j,k;
T temp;
for(i=0;i<last;i++)
{
k=i;
temp=slist[i]; get the first member data as temp data in one parision
for(j=i;j<=last;j++)
{
if(slist[j]<temp) data is smaller than the temp data
{
k=j;
temp=slist[j]; write the data to the temp data
}
}
if(k!=i) the temp data is not the first member data
{
temp=slist[i];
slist[i]=slist[k];
slist[k]=temp;
}
}
}
演示程式:
class Student
{
private:
int id;
string name;
char sex;
int age;
string address;
float eng,phy,electron,math;
public:
Student(){}
Student(int,string,char,int,string,float,float,float,float);
~Student(){}
bool operator<(Student);
void Show();
};
Student::Student(int i,string n,char s,int a,string add,float e,float p,float elec,float m)
{
id=i;
name=n;
sex=s;
age=a;
address=add;
eng=e;
phy=p;
electron=elec;
math=m;
}
void Student::Show()
{
cout<<se(8)<<id<<se(10)<<name<<se(5)<<sex<<se(4)<<age<<se(10)<<address<<se(4)<<eng<<se(4)<<phy<<se(4)<<electron<<se(4)<<math<<endl;
}
bool Student::operator<(Student stud)
{
return id<stud.id;
}
int main()
{
cout<<"This program is mainly used to show straight select sorting algorithm in the template of the orderedlist list"<<endl;
const int h=4;
int i;
orderedlist<Student,100> ordlist;
Student n[h]={Student(6004327,"zhangfei",'m',19,"beijing",80,85,90,78),
Student(6004121,"guanyu",'w',19,"shanghai",88,87,78,90),
Student(6004118,"liubei",'w',18,"tianjing",92,90,91,97),
Student(6004219,"zhaoyun",'m',18,"chongqin",86,79,76,80)};
for(i=0;i<h;i++)
{
ordlist.Insert(n[i],i);
}
cout<<"Before sorting:"<<endl;
ordlist.Print();
cout<<"After sorting:"<<endl;
ordlist.SelectSort();
ordlist.Print();
system("pause");
return 0;
}

CSDN部落格文章裡怎麼貼原始碼,即貼上的原始碼直接能拷貝。謝謝!

在程式碼編輯器裡選擇右鍵複製選單,到貼上的地方選擇右鍵貼上選單。程式碼編輯器本身只是一個純文字編輯器,他的操作和記事本沒有什麼差別。

求大神把c程式碼轉化成c++面向物件的程式碼!急求!線上等謝謝了!

用c++加一層 編譯成動態連結庫

急、急、求“分塊查詢”C++程式程式碼

#include<stdio.h>
#define n 16
#define m 4
typedef struct{int key; <br/> int ip; <br/> }indexelement;
typedef struct{int key; <br/> float info; <br/> }element;
typedef indexelement indextype[m];
typedef element sqlist[n];

int sequentialsearch(sqlist r,indextype index,int block,int k)
{
int i;
i=index[block].ip;
while(k!=r[i].key&&k<=index[block].key) i++;
if(k!=r[i].key) i=0;
return(i);
}

void main()
{
int i,j,k;
sqlist table;
indextype index;
printf("input %d element's key\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&j);
table[i].key=j;
}
printf("input %d element's ip to form an index'ip\n",m);
for(i=0;i<=m-1;i++)
{
scanf("%d",&j);
if(j<0||j>n)
{
printf("ERRORS!");

}
index[i].ip=j;
}
printf("input %d element's key to form the index'key\n",m);
for(i=0;i<=m-1;i++)
{
scanf("%d",&j);
index[i].key=j;
}
printf("search which key?\n");
scanf("%d",&k);
i=0;
while(k>index[i].key&&i<m-1) i++;
if(i>=m) printf("not fount\n");
else
{
j=sequentialsearch(table,index,i,k);
if(j==0) printf("not fount\n");
else printf("loc=%d\n",j);
}
}

求希爾排序法的C++程式碼

(2)希爾排序
void ShellInsert(int array[],long dk,long n)
{
int sentinel; 哨兵
for(long i=dk;i<n;i++){
if(array[i]<array[i-dk]){
sentinel=array[i];
for(long j=i-dk;(j>=0)&&(sentinel<array[j]);j-=dk)
array[j+dk]=array[j];
array[j+dk]=sentinel;
}
}
return;
}
void ShellSort(int array[],long n,long t) for(int i=0;i<t;i++)cout<<dlta[i]<<" ";cout<<endl;
{
long *dlta=new long[t];
if(t>0)dlta[0]=1;
long temp=1;
for(long i=1;i<t;i++){
dlta[i]=temp+1;
temp*=2;
}
for(i=t-1;i>=0;i--){
ShellInsert(array,dlta[i],n); dlta=...9,5,3,2,1也可以預先用一陣列存放好這個數列!
}
delete []dlta;
return;
}
endl of ShellSort
希望能給我分啦~

分页: 1 2 3
关于本站
读历史故事,了解历史人物,尽在历史新知网。