Tag Archives: Leadsort

Leadsort written in C

The sorting algorithm “leadsort” is a variant of “bubblesort” that allows you to sort the array in reverse. Computational complexity= O(n^2).

Source Code written in C language:


// © Codeido - www.codeido.com - Developed by Paolo Musolino

#include <stdio.h>

void swap(int *a, int *b)
{	
	int temp;
	temp=*a;
	*a=*b;
	*b=temp;
	
	
}
//---------------//---------------//---------------
int confronto(int x, int y)
{
	if(x<y) return -1;
	else if(x>y) return 1;
	else {
		return 0;
	}
	
}

void leadSort(int a[], int n)
{
	int i, j;
	
	for (i=n; i>1; i--) {
		for (j=1; j<i; j++) {
			if (confronto(a[j],a[j-1])<0) {
				swap(&a[j], &a[j-1]);
			}
		}
	}
	
}

int main ()
{
	int a[14]={5, 10, 11, 9, 7, 8, 4, 5, 7, 2, 45, 1, 4, 20};
	int i;
	leadSort(a, 14);
	printf("\n");
	for (i=0; i<14; i++) {
		printf("%d ", a[i]);
	}
	
	
	return 0;
}