C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

Sの中央値に最も近いk個の数を見つけるC++プログラム。ここで、Sはn個の数のセットです。


これは、Sの中央値に最も近いk個の数値を見つけるためのC ++プログラムです。ここで、Sはn個の数値のセットです。

アルゴリズム

Begin
   function partition() for partitioning the array on the basis of values at high as pivot value:
   Arguments:
      a[]=an array.
      l=low
   H=high
   Body of the function:
   Declare variables pivot, in, i
   Initialize in = l
   Set pivot = h
   For i=l to h-1
      if(a[i] < a[pivot])
         swap a[i] and a[in])
      increment in.
      swap a[pivot] and a[in]
   return in.
End
Begin
   function QuickSort() to implement quicksort algorithm to sort the data elements:
   Arguments:
      a[]=an array.
      l=low
      h=high
   Body of the function:
   declare pindex
   if(l < h)
      index = Partition(a, l, h)
      QuickSort(a, l, pindex-1);
      QuickSort(a, pindex+1, h);
   Return 0.
End
Begin
   Function main(),
   If the number of the data element are odd,
      Assign the middle index to low and the index next to it to high and calculate median.
      Run a loop for k times and print the element which his closer to the median.
   Else
      The median will be an average of two middle values .
      Run a loop for k times and print the element which his closer to the median.
End

#include<iostream>
using namespace std;
void swap(int *x, int *y) { //swapping two values 
   int tmp;
   tmp = *x;
   *x = *y;
   *y = tmp;
}
int Partition(int a[], int l, int h) {
   int pivot, in, i;
   in = l;
   pivot = h;
   for(i=l; i < h; i++) {
      if(a[i] < a[pivot]) {
         swap(&a[i], &a[in]);
         in++;
      }
   }
   swap(&a[pivot], &a[in]);
   return in;
}
int QuickSort(int a[], int l, int h) {
   int pindex;
   if(l < h) {
      pindex = Partition(a, l, h);
      QuickSort(a, l, pindex-1);
      QuickSort(a, pindex+1, h);
   }
   return 0;
}
int main() {
   int n, i, h, l, k;
   double d1,d2, median;
   cout<<"Enter the number of element in dataset: ";
   cin>>n;
   int a[n];
   for(i = 0; i < n; i++) {
      cout<<"\nEnter "<<i+1<<" element: ";
      cin>>a[i];
   }
   cout<<"\nEnter the number of element nearest to the median required: ";
   cin>>k;
   QuickSort(a, 0, n-1);
   cout<<"The K element nearest to the median are: ";
   if(n%2 == 1) {
      median = a[n/2];
      h = n/2+1;
      l= n/2;
      while(k > 0) {
         if((median-a[l] <= a[h]-median) && l >= 0) {
            cout<<" "<<a[l];
            l--;
            k--;
         } else if((median-a[l] > a[h]-median) && h <= n-1) {
            cout<<" "<<a[h];
            h++;
            k--;
         }
      }
   } else {
      d1 = a[n/2];
      d2 = a[n/2-1];
      median = (d1+d2)/2;
      h = n/2;
      l = n/2-1;
      while(k > 0) {
         d1 = a[l];
         d2 = a[h];
         if((median-d2 <= d1-median) && l >= 0) {
            cout<<" "<<a[l];
            l--;
            k--;
         } else if((median-d2 > d1-median) && h <= n-1) {
            cout<<" "<<a[h];
            h++;
            k--;
         }
      }
   }
   return 0;
}

出力

Enter the number of element in dataset: 7
Enter 1 element: 7
Enter 2 element: 6
Enter 3 element: 5
Enter 4 element: 4
Enter 5 element: 3
Enter 6 element: 2
Enter 7 element: 1
Enter the number of element nearest to the median required: 2
The K element nearest to the median are: 4 3

  1. 素集合データ構造を実装するC++プログラム

    互いに素なセットは、基本的に、複数のセットにアイテムを含めることができないセットのグループです。サブセットでのユニオンおよび検索操作をサポートします。 Find(): これは、特定の要素がどのサブセットに含まれているかを検索し、その特定のセットの代表を返すために使用されます。 Union(): 2つの異なるサブセットを1つのサブセットにマージし、1つのセットの代表が他のセットの代表になります。 関数と擬似コード Begin    Assume k is the element    makeset(k):      

  2. 要素が2つの異なる配列に格納されている要素の中央値を見つけるためのC++プログラム

    要素が2つの異なる配列に格納されている要素の中央値を見つけるために、C++プログラムを検討します。 アルゴリズム Begin Function Median()には、2つの配列a1 []、a2 []、およびn =引数としての配列の要素の数があります。範囲0〜 n、i =nの場合は実行し、n1:=n2 n2:=a2[0]はループを中断します。そうでない場合はj=nの場合、n1:=n2 n2:=a1[0]はa1[i] サンプルコード #include #include usingnamespace std; int Median(int a1 []、int a2 []、int n){int