我正在嘗試運行一個冒泡排序演算法,該演算法按升序對陣列進行排序,但它在在線編譯器中出現分段錯誤,我無法弄清楚那里出了什么問題,因為我認為陣列中的元素應該有四個大小,但在我嘗試后我找不到解決方案。有人可以幫我看看嗎?
#include <iostream>
#include <array>
using namespace std;
void bubble_sort(int arr[]);
void printArray(int arr[]);
int main()
{
int arr[] = {10, 4, 2, 8, 11, 15};
bubble_sort(arr);
printArray(arr);
// cout<<sizeof(arr)<<endl;
return 0;
}
void bubble_sort(int arr[])
{
for (int i = 0; i < sizeof(arr) / 4; i )
{
for (int j = 0; i < ((sizeof(arr) / 4) - 1); j )
{
int temp;
if (arr[j] > arr[j 1])
{
temp = arr[j];
arr[j] = arr[j 1];
arr[j 1] = temp;
}
}
}
}
void printArray(int arr[])
{
for (int i = 0; i < (sizeof(arr) / 4); i )
{
cout << arr[i] << endl;
}
cout << "\n";
}
uj5u.com熱心網友回復:
bubble_sort 中嵌套的 for 回圈的終止條件為i < ((sizeof(arr) / 4) - 1). 因為變數i在嵌套回圈中永遠不會遞增,所以它將永遠回圈。試試j < ((sizeof(arr) / 4) - 1)吧。這就是導致您的分段錯誤的原因。
我還建議您獲取陣列的大小并將其作為單獨的引數傳遞給您的函式,而不是嘗試sizeof從函式內部使用。正如“一些程式員老兄”所提到的,該sizeof函式當前使用的是 a 的大小*int,而不是陣列中的元素數。sizeof(arr)在您的main功能中將解決此問題。
(這是我在 Stack Overflow 上的第一個回答,如有任何格式錯誤請見諒。)
uj5u.com熱心網友回復:
有更好的方法來處理 C 中的陣列,例如
#include <algorithm>
#include <iostream>
//---------------------------------------------------------------------------------------------------------------------
//
// using namespace std; <== unlearn to do this.
//
//---------------------------------------------------------------------------------------------------------------------
// Instead of having to do sizeof(arr)/sizeof(arr[0]) and trying to deal with pointer decay/losing size informatino of the array :
// Use this syntax int (&arr)[N] to pass an array by reference AND its size N
// (or use std::array<int,N>&)
// I made this a function template so the function will compile for any N
// I also replaced int by std::size_t since that's the common indexing type
// in collections (and unlike int it cant get to negative indices)
template<std::size_t N>
void bubble_sort(int(&arr)[N])
{
for (std::size_t i = 0; i < N; i ) // no need to divide by sizeof(int)
{
for (std::size_t j = 0; j < N - 1; j ) // <== you had an i here in the comparisson in your original code, also a bug
{
if (arr[j] > arr[j 1])
{
std::swap(arr[j], arr[j 1]); // using swap make code explain itself
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
template<std::size_t N>
void printArray(const int(&arr)[N]) // pass by const content off arr must not be modifiable by print
{
bool comma = false;
std::cout << "[";
// use range based for loop, can't go out of bound.
for (const auto value : arr)
{
if (comma) std::cout << ",";
std::cout << value;
comma = true;
}
std::cout << "]\n"; // preferably don't use std::endl (it will flush and slow output down)
}
//---------------------------------------------------------------------------------------------------------------------
int main()
{
int arr[]{ 10, 4, 2, 8, 11, 15 };
bubble_sort(arr);
printArray(arr);
return 0;
}
uj5u.com熱心網友回復:
現代 C 冒泡排序方法:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <ranges>
template <std::ranges::bidirectional_range R>
void bubble_sort(R&& r) {
while (true) {
auto it = std::ranges::is_sorted_until(r);
if (it == std::end(r)) {
break;
}
std::iter_swap(it, std::prev(it));
}
}
template <std::ranges::forward_range R>
void print(R&& r) {
for (auto it = std::begin(r); it != std::end(r); it) {
std::cout << *it << ' ';
}
std::cout << '\n';
}
int main() {
int arr[] = {10, 4, 2, 8, 11, 15};
bubble_sort(arr);
print(arr);
}
演示:https : //wandbox.org/permlink/Co4k1GA8ozQ9PfDv
注意:
- 您不需要在這里知道陣列的大小
- 該演算法不限于 C 風格的陣列,您可以使用(雙重)鏈表、向量、跨度、二叉樹或其他任何東西
- 代碼更具“描述性”;您會找到“排序到的位置”,以及“如果該點位于范圍的末尾則停止”,如果不是,則“與該點之前的內容交換”
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/420582.html
標籤:
