C語言中檢查陣列是否已排序的程式(迭代和遞迴)
給定一個包含 n 個元素的陣列 arr[],我們的任務是檢查給定的陣列是否按排序順序排列。如果它按排序順序排列,則列印“陣列按排序順序排列”,否則列印“陣列未按排序順序排列”。
為了解決上述問題,我們可以使用迭代或遞迴方法,我們將討論這兩種方法。
遞迴方法
那麼,什麼是遞迴方法?在遞迴方法中,我們反覆呼叫函式,直到得到期望的結果。在遞迴方法中,函式返回的值儲存在棧記憶體中。
輸入
arr[] = {12, 13, 14, 16, 18}輸出
The array is in sorted order
解釋 - 12 < 13 < 14 < 16 < 18,所以,是的,陣列已排序
輸入
arr[] = {2, 1, 3, 5, 6}輸出
The array is not in sorted order
解釋 - 2 不小於 1,所以,它沒有按排序順序排列。
下面使用的解決問題的方法如下:
將陣列 arr[] 作為輸入,並用陣列的大小初始化 n。
檢查是否到達陣列的開頭,返回 true。
檢查陣列的前一個元素是否不小於下一個元素,返回 false。
遞減 n 並轉到步驟 2。
演算法
Start
In function int arraySortedCheck(int arr[], int n)
Step 1→ If n == 1 || n == 0 then,
Return 1
Step 2→ If arr[n-1] < arr[n-2] then,
Return 0
Step 3→ Return arraySortedCheck(arr, n-1)
In Function int main(int argc, char const *argv[])
Step 1→ Declare and initialize arr[] as {1,8,3,4,7}
Step 2→ Declare and initialize int n as sizeof(arr)/sizeof(arr[0])
Step 3→ If arraySortedCheck(arr, n) then,
Print "Array is in sorted order”
Step 4→ Else
Print "Array is not in sorted order”
Stop示例
//Recursive approach
#include <stdio.h>
//Recursive function to check if it
//is in sorted order or not
int arraySortedCheck(int arr[], int n){
//all elements are checked and
//all are in sorted order
if (n == 1 || n == 0)
return 1;
//when an array is not in sorted order
if(arr[n-1] < arr[n-2])
return 0;
return arraySortedCheck(arr, n-1);
}
int main(int argc, char const *argv[]){
int arr[] = {1,8,3,4,7};
int n = sizeof(arr)/sizeof(arr[0]);
if(arraySortedCheck(arr, n)){
printf("Array is in sorted order
");
}
else
printf("Array is not in sorted order
");
return 0;
}輸出
如果執行上述程式碼,它將生成以下輸出:
Array is in sorted order
迭代方法
在迭代方法中,我們使用 for 迴圈、while 迴圈或 do-while 迴圈,這些迴圈執行語句,直到條件成立,這意味著 1。
輸入
arr[] = {12, 13, 14, 16, 18}輸出
The array is in sorted order
解釋 - 12 < 13 < 14 < 16 < 18,所以,是的,陣列已排序
輸入
arr[] = {2, 1, 3, 5, 6}輸出
The array is not in sorted order
解釋 2 不小於 1,所以,它沒有按排序順序排列。
下面使用的解決問題的方法如下
輸入 arr[]。
迴圈直到到達該陣列的末尾。
檢查當前元素是否不小於下一個元素,返回 false 並退出。
否則繼續。
轉到步驟 2。
演算法
Start
In function int arraySortedCheck(int arr[], int n)
Step 1 → Loop For i = 0 and i < n and ++i
If arr[n-1] < arr[n-2] then,
Return 0
Step 2→ Return 1
In Function int main(int argc, char const *argv[])
Step 1→ Declare and initialize arr[] as {1,8,3,4,7}
Step 2→ Declare and initialize int n as sizeof(arr)/sizeof(arr[0])
Step 3→ If arraySortedCheck(arr, n) then,
Print "Array is in sorted order”
Step 4→ Else
Print "Array is not in sorted order”
Stop示例
//Iterative approach
#include <stdio.h>
int arraySortedCheck(int arr[], int n){
for (int i = 0; i < n; ++i){
//when an array is not in sorted order
if(arr[n-1] < arr[n-2])
return 0;
}
//all elements are checked and
//all are in sorted order
return 1;
}
int main(int argc, char const *argv[]){
int arr[] = {1,8,3,4,7};
int n = sizeof(arr)/sizeof(arr[0]);
if(arraySortedCheck(arr, n)){
printf("Array is in sorted order
");
}
else
printf("Array is not in sorted order
");
return 0;
}輸出
如果執行上述程式碼,它將生成以下輸出:
Array is in sorted order
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP