Data structures and algorithmic sorting (bubbling selection insertion)

  • 2020-05-26 09:52:29
  • OfStack

Data structures and algorithmic sorting (bubbling, selection, insertion)

Bubble sort

1.1 algorithm

The bubble sort (buddle-sort) algorithm works as follows :(from back to front)

Compare adjacent elements. If the first one is bigger than the second one, you swap them.

Do the same for each pair of adjacent elements, starting with the first pair and ending with the last pair. At this point, the last element is going to be the largest number.

Repeat the above steps for all elements except the last one.

Keep repeating the above steps for fewer and fewer elements each time until there are no pairs of Numbers to compare.

1.2 implementation


// 
// main.c 
// BubbleSort 
// 
// Created by Wuyixin on 2017/6/2. 
// Copyright © 2017 years  Coding365. All rights reserved. 
// 
 
#include <stdio.h> 
 
void bubbleSort(int a[],int n){ 
  int i,j; 
  for (i = 0; i < n - 1; i++) { 
    for (j = 0; j < n - i; j++) { 
      if (a[j] > a[j + 1]){ 
        int temp = a[j]; 
        a[j] = a[j + 1]; 
        a[j + 1] = temp; 
      } 
    } 
  } 
} 
 
int main(int argc, const char * argv[]) { 
   
   
  int a[] = {9,3,1,4,7,6,5,8,2}; 
  bubbleSort(a, 9); 
  int i = 0; 
  while (i < 9) 
    printf("%d ",a[i++]); 
   
  return 0; 
} 

2. Select sort

2.1 algorithm

Selection sort (selection-sort) is a simple and intuitive sorting algorithm. It works by selecting the smallest (or largest) element from the data element to be sorted every time and storing it at the beginning of the sequence until all the data elements to be sorted are sorted

2.2 implementation


// 
// main.c 
// SelectionSort 
// 
// Created by Wuyixin on 2017/6/2. 
// Copyright © 2017 years  Coding365. All rights reserved. 
// 
 
#include <stdio.h> 
 
void selectionSort(int a[],int n){ 
  int i,j,min,temp; 
   
  for (i = 0; i < n; i++) { 
    min = i; 
    for (j = i + 1; j < n; j++) { 
      if (a[j] < a[min]) 
        min = j; 
    } 
    if (i != min){ 
      temp = a[i]; 
      a[i] = a[min]; 
      a[min] = temp; 
    } 
  } 
} 
 
int main(int argc, const char * argv[]) { 
  int a[] = {9,3,1,4,7,6,5,8,2}; 
  selectionSort(a, 9); 
  int i = 0; 
  while (i < 9) 
    printf("%d ",a[i++]); 
  return 0; 
} 



3. Insert sort

3.1 algorithm

The basic idea of insertion sort (insertion-sort) is to insert one record to be sorted into the previously sorted file at the appropriate position according to its key value at each step until all the records are inserted.

3.2 implementation


// 
// main.c 
// InsertionSort 
// 
// Created by Wuyixin on 2017/6/2. 
// Copyright © 2017 years  Coding365. All rights reserved. 
// 
 
#include <stdio.h> 
 
void insertionSort(int a[],int n){ 
  int i,j,temp; 
  for (i = 1; i < n ; i++) { 
    temp = a[i]; 
    for (j = i; j > 0 && temp < a[j - 1]; j--) { 
      a[j] = a[j - 1]; 
    } 
    a[j] = temp; 
  } 
} 
 
int main(int argc, const char * argv[]) { 
  int a[] = {9,3,1,4,7,6,5,8,2}; 
  insertionSort(a, 9); 
  int i = 0; 
  while (i < 9) 
    printf("%d ",a[i++]); 
  return 0; 
} 

The above is the C language data structure and algorithm sorting explanation, if you have any questions can leave a message or to the site community exchange discussion, thank you for reading, hope to help you, thank you for the support of the site!


Related articles: