Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void draw(int N) {
- int k = 0;
- int n = 0;
- while (k < N) {
- n = 0;
- while (n <= k) {
- cout << "**";
- n++;
- }
- cout << endl;
- k++;
- }
- }
- void genArr(int sz, int arr[]) {
- for (int k = 0; k < sz; k++) {
- arr[k] = 10 + rand() % 90;
- }
- }
- void showArr(int sz, int arr[]) {
- for (int k = 0; k < sz; k++)
- cout << arr[k] << " ";
- cout << endl;
- }
- void insertSortB(long sz, int arr[]) {
- int tmp;
- long i, j;
- for (i = 1; i < sz; i++) {
- tmp = arr[i];
- j = i - 1;
- while (j >= 0 && arr[j] > tmp) {
- arr[j + 1] = arr[j];
- j = j - 1;
- }
- arr[j + 1] = tmp;
- //showArr(sz, arr);
- //getchar();
- }
- }
- void insertSortS(long sz, int arr[]) {
- int tmp;
- long i, j;
- for (i = 1; i < sz; i++) {
- tmp = arr[i];
- j = i - 1;
- while (j >= 0 && arr[j] < tmp) {
- arr[j + 1] = arr[j];
- j = j - 1;
- }
- arr[j + 1] = tmp;
- //showArr(sz, arr);
- //getchar();
- }
- }
- void copyArr(long sz, int src[], int dst[]) {
- for(int k = 0; k < sz; k++)
- dst[k] = src[k];
- }
- int searchArr(int val, long sz, int arr[]) {
- for (int k = 0; k < sz; k++) {
- if (arr[k] == val) {
- return k;
- }
- }
- return -1;
- }
- void compSort(int val, long sz, int arr[]) {
- int id = searchArr(val, sz, arr);
- if (id >= 0) {
- insertSortB(id, arr);
- insertSortS(sz-id, &(arr[id+1]));
- }
- }
- int main(){
- const int sz = 10;
- int arr[sz], arr2[sz]{};
- genArr(sz, arr);
- copyArr(sz, arr, arr2);
- compSort(74, sz, arr2);
- //insertSortB(sz, arr);
- //insertSortS(sz, arr2);
- showArr(sz, arr);
- showArr(sz, arr2);
- //cout << searchArr(74, sz, arr) << " "
- // << searchArr(74, sz, arr2) << endl;
- /*
- insertSortB(sz, arr);
- showArr(sz, arr);
- */
- //draw(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement