Reputation: 31
#include <iostream>
using namespace std;
void selecao(float A[], int n)
{
int i, j, indicmin;
float auxiliar;
for (i = 0; n-1; i++)
{
indicmin = i;
for(j = i+1 ; j< n; j++)
if (A[j] < A[indicmin])
indicmin = j;
auxiliar = A[indicmin];
A[indicmin] = A[i];
A[i] = auxiliar;
}
}
// Imprimir a lista em ordem crescente
void ImprimirLista (float a[], int n)
{
for (int i = 0 ; i < n ; i++)
cout << a[i] << " ";
cout << endl ;
}
int main()
{
float A[4]={1.0,4.0,3.0,0.0};
selecao(A,4);
ImprimirLista(A,4);
}
I nees this to form a list in crescent order, so to test I create the ImprimirLista and main, but the result is nothing. Don't print anything.
Upvotes: 1
Views: 78
Reputation: 464
I think what you are trying to do is a Selection Sort. General form:
void selectSort(int* a, int n)
{
int minIdx, minValue;
for(int i = 0; i < n; i++)
{
minIdx = i;
minValue = a[i];
for(int j = i+1; j < n; j++)
{
if(a[j] < minValue)
{
minIdx = j;
minValue = a[j];
}
}
if(minIdx != i)
{
int temp = a[i];
a[i] = a[minIdx];
a[minIdx] = temp;
}
}
}
Your Code:
void selecao(float A[], int n)
{
int i, j, indicmin;
float min;
float auxiliar;
for (i = 0; i < n; i++)
{
indicmin = i;
min = A[i];
for(j = i+1 ; j< n; j++)
{
if (A[j] < min)
{
indicmin = j;
min = A[j];
}
}
if(indicmin != i)
{
auxiliar = A[indicmin];
A[indicmin] = A[i];
A[i] = auxiliar;
}
}
}
Upvotes: 0
Reputation: 1150
using namespace std;
void selecao(float* A, int n)
{
int i, j, indicmin;
float auxiliar;
for (i = 0; i <= n - 1; i++)
{
indicmin = i;
for (j = i + 1; j < n; j++)
{
if (A[j] < A[indicmin])
indicmin = j;
}
auxiliar = A[indicmin];
A[indicmin] = A[i];
A[i] = auxiliar;
}
}
In main you need below change:
selecao(&A[0], 4);
Why your program was not working? First for loop comparison was wrong as well as array passed wrongly.
Upvotes: 1
Reputation: 3018
What about using the stl
?
#include <algorithm>
int main()
{
float v[4] {1.0f, 4.0f, 3.0f, 0.0f};
std::sort(v,v+v_size);
return 0;
}
Upvotes: 0