Direct selection sort and bubble sort

#include <stdlib.h> // contain declarations of malloc memory allocation functions

#include "sort.h"

// direct insertion sort algorithm modified (non-adaptive, regardless of a comparison result between [i-1] and a [I])

void insertion (Item a [], int l, int r) {// l, r interval to be sorted are lower and upper bounds subscripts

int i,j;

       for (i=l+1; i<=r; i++)

              for (j=i; j>l; j--)

                     compexch(a[j-1], a[j]);

}

// Direct Selection Sort

void selection (Item a[], int l, int r ) {

you i, j, min;

       for (i=l; i<r; i++) {

              min = i;

              for (j = i+1; j <=r ; j++) 

                     if ( less(a[j], a[min]) ) min=j;       

              exch(a[i], a[min]);

       }

}

//Bubble Sort

void bubble(Item a[], int l, int r) {

int i,j;

       for (i=l; i<r; i++) {

              for (j=r; j>i; j--)

                     compexch(a[j-1], a[j]);

       }

}

Guess you like

Origin www.cnblogs.com/huqian477/p/huqian3.html