参考链接“:https://blog.csdn.net/sinat_31425585/article/details/74612768
sort.cpp
#include <vector>
#include <iostream>
using namespace std;
void BubbleSort(float *p, int length, int * ind_diff)
{
for (int m = 0; m < length; m++)
{
ind_diff[m] = m;
}
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length- i - 1; j++)
{
if (p[j] > p[j + 1])
{
float temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
int ind_temp = ind_diff[j];
ind_diff[j] = ind_diff[j + 1];
ind_diff[j + 1] = ind_temp;
}
}
}
}
int main()
{
float a[8] = { 1.0, 1.8, 1.5, 1.1, 4.3, 5.5, 2.2, 8.8 };
int ind[8] = { 0 };
BubbleSort(a, 8, ind);
for (int i = 0; i < 8; i++)
{
cout << "value: " << a[i] << " Index: " << ind[i] << endl;
}
return 0;
}
./a.out 测试输出
bzl@bzl-Latitude-5591:~/Documents/test$ g++ sort.cpp
bzl@bzl-Latitude-5591:~/Documents/test$ ./a.out
value: 1 Index: 0
value: 1.1 Index: 3
value: 1.5 Index: 2
value: 1.8 Index: 1
value: 2.2 Index: 6
value: 4.3 Index: 4
value: 5.5 Index: 5
value: 8.8 Index: 7
bzl@bzl-Latitude-5591:~/Documents/test$