C++ insertion sorting elements -


other way elements in function instead of main program?

void insertionsort(int array[], int number) {      int j, temp;     (int = 1; i<number; i++)     {         j = i;         while (j>0 && array[j - 1]>array[j])         {             temp = array[j];             array[j] = array[j - 1];             array[j - 1] = temp;              j--;         }     }  }    int main() {      int number = 8;      int array[] = { 2, 7, 5, 6, 4, 8, 1, 3 };      insertionsort(array, 8);      (int = 0; i<number; i++)         cout << array[i] << " ";     cout << endl;      system("pause");      return 0; } 

while data sorted could moved sort function, doing creates function that's pretty useless--since ever sorts 1 set of data, it's equivalent return {1, 2, 3, 4 5, 6, 7, 8};

your insertion sort bit of mess. pseudo-code insertion sort looks this:

for in 1 size      temp = array[i]     j in downto 0 , array[j-1] > temp         array[j] = array[j-1]     array[j] = temp 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -