You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<stdio.h>voidprintArray(intarr[], intarrLength);
voidbubbleSort(intarr[], intarrLength);
intmain(void) {
intx[] = { 5, 4, 3, 2, 1 };
intarrLength=sizeof(x)/sizeof(x[0]);
bubbleSort(x, arrLength);
printArray(x, arrLength);
}
voidprintArray(intarr[], intarrLength) {
printf("Array: ");
for (inti=0; i<arrLength; i++) {
printf("%i", arr[i]);
// add space after all, except lastif (i<arrLength-1) {
printf(" ");
}
}
printf(".\n");
}
voidbubbleSort(intarr[], intarrLength) {
intlastIndex=arrLength-1;
// anything after 'i' is already sorted// except first element right after it,// this one will be compared with the lastIndexfor (inti=lastIndex; i >= 0; i--) {
for (intj=0; j<i; j++) {
intcurrent=arr[j];
intnext=arr[j+1];
// if elements are out of order, swap themif (current>next) {
arr[j] =next;
arr[j+1] =current;
}
}
}
}
Insertion sort
#include<stdio.h>voidprintArray(intarr[], intarrLength);
voidarrSplice(intarr[], intoldIndex, intnewIndex);
voidinsertionSort(intarr[], intarrLength);
intmain(void) {
intx[] = { 5, 4, 3, 2, 1 };
intarrLength=sizeof(x)/sizeof(x[0]);
insertionSort(x, arrLength);
printArray(x, arrLength);
}
voidprintArray(intarr[], intarrLength) {
printf("Array: ");
for (inti=0; i<arrLength; i++) {
printf("%i", arr[i]);
// add space after all, except lastif (i<arrLength-1) {
printf(" ");
}
}
printf(".\n");
}
voidarrSplice(intarr[], intoldIndex, intnewIndex) {
// number at oldIndex will be placed in the newIndex index// and everything from the newIndex until right before the oldIndex position// will be move one index to the right eachintsorting=arr[oldIndex];
for (inti=oldIndex-1; i >= newIndex; i--) {
arr[i+1] =arr[i];
}
arr[newIndex] =sorting;
}
voidinsertionSort(intarr[], intarrLength) {
for (inti=0; i<arrLength; i++) {
// current number being sortedintsorting=arr[i];
// compare 'sorting' with every number of the sorted part// until a larger number is found or until the end of the loop// if no larger number is founded// 'sorting' is already in the right placefor (intj=0; j<i; j++) {
// if a larger number than 'sorting' is found// 'sorting' will be inserted right before itif (sorting<arr[j]) {
arrSplice(arr, i, j);
break;
}
}
}
}
Selection sort
#include<stdio.h>voidprintArray(intarr[], intarrLength);
voidselectionSort(intarr[], intarrLength);
intmain(void) {
intx[] = { 5, 4, 3, 2, 1 };
intarrLength=sizeof(x)/sizeof(x[0]);
selectionSort(x, arrLength);
printArray(x, arrLength);
}
voidprintArray(intarr[], intarrLength) {
printf("Array: ");
for (inti=0; i<arrLength; i++) {
printf("%i", arr[i]);
// add space after all, except lastif (i<arrLength-1) {
printf(" ");
}
}
printf(".\n");
}
voidselectionSort(intarr[], intarrLength) {
// anything before 'i'// is considered the sorted partfor (inti=0; i<arrLength; i++) {
// find the smallest number in the unsorted partintsmallestIndex=i;
intsmallestValue=arr[i];
for (intj=i+1; j<arrLength; j++) {
if (arr[j] <smallestValue) {
smallestIndex=j;
smallestValue=arr[j];
}
}
// swap the smallest number in the unsorted part// with the first number in the unsorted partarr[smallestIndex] =arr[i];
arr[i] =smallestValue;
}
}
Merge sort
#include<stdio.h>#include<math.h>voidprintArr(intarr[], intarrLength);
voidmergeSort(intarr[], intfirstIndex, intlastIndex);
voidmergeSortParts(intarr[], intfirstIndex, intmiddleIndex, intlastIndex);
intmain(void) {
intarr[] = { 4, 6, 2, 1, 3, 5 };
intlen=sizeof(arr) / sizeof(arr[0]);
printArr(arr, len);
mergeSort(arr, 0, len-1);
printArr(arr, len);
}
voidprintArr(intarr[], intarrLength) {
printf("Array: ");
for (inti=0; i<arrLength; i++) {
printf("%i", arr[i]);
// add space after all, except lastif (i<arrLength-1) {
printf(" ");
}
}
printf(".\n");
}
voidmergeSortParts(intarr[], intfirstIndex, intmiddleIndex, intlastIndex) {
inttotalLen=lastIndex-firstIndex+1;
inttempArr[totalLen];
// range: firstIndex to middleIndexintcurLeft=firstIndex;
// range: middleIndex + 1 to lastIndexintcurRight=middleIndex+1;
for (inti=0; i<totalLen; i++) {
// if all itens in the left part// were already placed into 'tempArr'// catch next at the right partif (curLeft>middleIndex) {
tempArr[i] =arr[curRight];
curRight=curRight+1;
// if all itens in the right part// were already placed into 'tempArr'// catch next at the left part
} elseif (curRight>lastIndex) {
tempArr[i] =arr[curLeft];
curLeft=curLeft+1;
} else {
// otherwise, if both parts still have available items// compare them and get smallerif (arr[curLeft] <arr[curRight]) {
tempArr[i] =arr[curLeft];
curLeft=curLeft+1;
} else {
tempArr[i] =arr[curRight];
curRight=curRight+1;
}
}
}
// copy the tempArr to its respective position at arrfor (inti=0; i<totalLen; i++) {
arr[firstIndex+i] =tempArr[i];
}
}
voidmergeSort(intarr[], intfirstIndex, intlastIndex) {
// recursion// if current subarray is not of length 1,// i.e. its first element and last element doesn't have the same index,// split array in half, and call this function with each halfif (firstIndex!=lastIndex) {
intmiddleIndex=floor(firstIndex+ (lastIndex-firstIndex) / 2);
mergeSort(arr, firstIndex, middleIndex);
mergeSort(arr, middleIndex+1, lastIndex);
mergeSortParts(arr, firstIndex, middleIndex, lastIndex);
}
}