Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ เพื่อค้นหาองค์ประกอบขั้นต่ำในอาร์เรย์โดยใช้การค้นหาเชิงเส้น


นี่คือโปรแกรม C++ เพื่อค้นหาองค์ประกอบขั้นต่ำของอาร์เรย์โดยใช้วิธีการค้นหาแบบเชิงเส้น ความซับซ้อนของเวลาของโปรแกรมนี้คือ O(n)

อัลกอริทึม

Begin
   Assign the data element to an array.
   Assign the value at ‘0’ index to minimum variable.
   Compare minimum with other data element sequentially.
   Swap values if minimum value is more then the value at that particular index of the array.
   print the minimum value.
End

โค้ดตัวอย่าง

#include<iostream>
using namespace std;
int main() {
   int n, i, minimum, a[10] = {1, 6, 7, 10, 12, 14, 12, 16, 20, 26};
   char ch;
   minimum = a[0];
   cout<<"\nThe data element of array:";
   for(i = 0; i < 10; i++) {
      cout<<" "<<a[i];
      if(minimum > a[i])
         minimum= a[i];
   }
   cout<<"\n\nMinimum of the data elements of array using linear search is: "<<minimum;
   return 0;
}

ผลลัพธ์

The data element of array: 1 6 7 10 12 14 12 16 20 26
Minimum of the data elements of array using linear search is: 1