Direct insertion sort - implemented method of object-oriented (simple)

Source:

#include <iostream>

#define N 5

using namespace std;

 

class Insert

{

private:

int a[N];

public:

Insert (int m [], int n) // constructor initializes an array of private variables

{

for (int i = 0; i < n; i++)

a[i] = m[i];

}

void insert_sort (); // member function for ordering

void show (); // member function for display

};

void Insert :: show () // member function show () definition

{

for (int i = 0; i < N; i++)

cout << a[i] << "  ";

}

void Insert :: insert_sort () // direct insertion sort

{

int i, j, temp;

for (i = 1; i < N; i++)

{

temp = a[i];

j = i - 1;

while (j >= 0 && temp < a[j])

{

a[j + 1] = a[j];

j--;

}

a[j + 1] = temp;

}

}

int main ()

{

int *pArray;

int i;

pArray = new int [N]; // dynamically allocated arrays

// input to the array of five integers

for (i = 0; i < N; i++)

cin >> pArray[i];

Insert B (pArray, N); // class definition object, and initializes the object

B.insert_sort (); // object reference member function

B.show (); // object reference member function

system("pause");

return 0;

}

operation result:

Guess you like

Origin www.cnblogs.com/duanqibo/p/11972567.html
Recommended