本文介绍了选择排序..帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨..
我正在尝试实施选择排序的算法步骤。
1 - >找到最低指数
2 - >与Top&增量。
输出不正确..请有一个L ^ _ ^ K. >!?
void in ( int arr [], int size)
{
for ( int i = 0 ; i< size; i ++)
{
cout<< 元素#<< i + 1<< :;
cin>> ARR [I];
}
}
void out( int arr [], int size)
{
for ( int j = 0 ; j< size; j ++)
{
cout<< arr [j]<< ||;
}
}
int find_min( int arr [], int size)
{
int index = 0 跨度>;
for ( int k = 0 ; k< size; k ++)
{
if (arr [index]> arr [k])
{
index = k;
}
}
return index;
}
void exchange( int & x, int & y)
{
int temp = x;
x = y;
y = temp;
}
void selection_sort( int arr [], int size)
{
int loc;
for ( int i = 0 ; i< size; i ++)
{
loc = find_min(arr,size - i);
exchange(arr [loc],arr [i]);
}
}
int main()
{
const int size = 5 ;
int my_array [size];
in (my_array,size);
out(my_array,size);
cout<< 选择后排序..<< \ n \ n;
selection_sort(my_array,size);
cout<< \ n\t<< p r i n t g ...<< ENDL;
out(my_array,size);
return 0 ;
}
= =>更新(V2)
int find_min( int ar [], int size)
{
int index = 0 ;
bool found = false ;
for ( int i = 1 ; i< size; i ++)
{
if (ar [index]> ar [i])
{
index = i;
found = true ;
}
}
如果(找到)
{
return index;
}
else
return - 1 跨度>;
}
void selection_sort( int arr [], int size)
{
int loc;
for ( int count = 0 ; count< size; count ++)
{
loc = find_min(arr,size - count);
if (loc> = 0 )
{
exchange (arr [count],arr [loc]);
}
}
}
解决方案
Hi ..
I'm Trying To Implement The Algorithmic Steps of Selection Sort.
1 -> Find The Min Index
2 -> Exchange It With The Top & Increment.
The Output Is Not Correct .. Please Have A L^_^K. >!?
void in(int arr[], int size) { for(int i = 0; i < size; i++) { cout << "Element # " << i+1 << ": "; cin >> arr[i]; } } void out(int arr[], int size) { for(int j = 0; j < size; j++) { cout << arr[j] << "||"; } } int find_min(int arr[], int size) { int index = 0; for(int k = 0; k < size; k++) { if( arr[index] > arr[k]) { index = k; } } return index; } void exchange(int &x, int &y) { int temp = x; x = y; y = temp; } void selection_sort(int arr[], int size) { int loc; for(int i = 0; i < size; i++) { loc = find_min(arr, size - i); exchange(arr[loc], arr[i]); } } int main() { const int size = 5; int my_array[size]; in(my_array, size); out(my_array, size); cout << "After Selection Sort.." << "\n\n"; selection_sort(my_array, size); cout << "\n\t" << "p r i n t i n g.." << endl; out(my_array, size); return 0; }
= = > Update(V2)
int find_min(int ar[], int size) { int index = 0; bool found = false; for(int i = 1; i < size; i++) { if(ar[index] > ar[i]) { index = i; found = true; } } if (found) { return index; } else return -1; } void selection_sort(int arr[], int size) { int loc; for(int count = 0; count < size; count++) { loc = find_min(arr, size - count); if (loc >= 0) { exchange(arr[count], arr[loc]); } } }
解决方案
这篇关于选择排序..帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!