int b,d,i,n,j; int a = Memo1->Lines->Count; int* Mas1 = new int[a]; b=a-1; //Zdes' sortirovka massiva raznimi sposobami for (i=0;i<=b;i++) { Memo1->Lines->Strings[i]=Mas1[i]; } delete [] Mas1; 

Already made the "bubble" method max The "bubble" method min Sorting through Strlist With qSort you will sort it out slowly, but "Sorting by inserts" "Sorting through selection" doesn’t work. you are welcome.

2 answers 2

Not very clear what kind of Memo in question. Sorting by inserts is insertionSort (array, the number of elements in it), and the choice is selection (array, left border, right border):

 #include <stdio.h> #include <stdlib.h> void insertionSort(int arr[], int length) { int i, j, tmp; for (i = 1; i < length; i++) { j = i; while (j > 0 && arr[j - 1] > arr[j]) { tmp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = tmp; j--; } } } void selection(int a[], int l, int r) { for (int i = l; i < r; i++) { int min = i; for (int j = i + 1; j <= r; j++) { if (a[j] < a[min]) min = j; } if (min != i) { int t = a[i]; a[i] = a[min]; a[min] = t; } } } int main() { int n = 10, *arr; arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = rand() % 100; printf("%d\n", arr[i]); } // insertionSort(arr, n); // selection(arr, 0, n-1); printf("\n\n"); for (int i = 0; i < n; i++) { printf("%d\n", arr[i]); } } 

Naturally, to test it is necessary to uncomment one of the lines.

     #include <stdio.h> #include <stdlib.h> main () { int a[10] = {3,4,1,5,6,8,8,2,4,9}; int i, j, t; for (i = 8; i >= 0; i--) { for (j = i; j < 9; j++) { if (a[j] <= a[j+1]) break; t=a[j]; a[j] = a[j+1]; a[j+1] = t; } } for (i = 0; i < 10; i++) printf ("%d ",a[i]); printf ("\nEnd\n"); } 

    This is sorting by inserts in ascending order.

    The idea is that in the inner loop a new element is inserted into an already ordered sequence . At the first step, its length is 1. The insertion takes place through an exchange, plunging a new element into each already sorted one at each step, until it takes its place.

    The outer loop supplies new items. As a function for an array of size N, I think, issue it yourself.

    Which of the algorithms is called "Sort by choice" I do not remember.

    If the topic is interesting, I advise you to twist 1). Sorting inserts into the tree (the list structure is ordered at each step. It is convenient when searching for an unspecified number of items comes in) 2). Sort merge (especially for linked lists).

    • Why count i begins with 8, - 1 element.? - Afimida
    • Only 10 items. The index of the latter, respectively 9. It is already an ordered sequence of one element. Before it is an element with an index of 8. We start inserts with it, we end with a zero. I hope clearly explained. - avp