美文网首页
各种排序方法

各种排序方法

作者: crabor | 来源:发表于2018-02-23 17:23 被阅读0次

冒泡排序法

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define M 50

void BubbleSort(int a[], int n) {
  int i, j;
  int temp;

  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < n - i - 1; j++) {
      temp = a[j];
      a[j] = a[j] < a[j + 1] ? a[j] : a[j + 1];
      if (a[j] != temp) {
        a[j + 1] = temp;
      }
    }
  }
}

int main(void) {
  int a[M];
  int i;
  srand((unsigned)time(NULL));
  for (i = 0; i < M; i++) {
  a[i] = rand() % 101;
  printf("%d ", a[i]);
  }
  printf("\n");
  BubbleSort(a,M);
  for (i = 0; i < M; i++) {
  printf("%d ",a[i]);
  }
  return 0;
}

选择排序法

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define M 50

int *MinPointer(int a[], int n) {
  int *pMin;
  int i;
  pMin = &a[0];
  for (i = 0; i < n; i++) {
    if (*pMin > a[i]) {
      pMin = &a[i];
    }
  }
  return pMin;
}

void SelectionSort(int a[], int n) {
  int i, temp;
  int *pMin;
  for (i = 0; i < n - 1; i++) {
    pMin = MinPointer(&a[i], n - i);
    temp = a[i];
    a[i] = *pMin;
    *pMin = temp;
  }
}


int main(void) {
  int a[M];
  int i;
  srand((unsigned)time(NULL));
  for (i = 0; i < M; i++) {
  a[i] = rand() % 101;
  printf("%d ", a[i]);
  }
  printf("\n");
  SelectionSort(a,M);
  for (i = 0; i < M; i++) {
  printf("%d ",a[i]);
  }
  return 0;
}

链表排序法

#include <stdio.h>
#include<stdlib.h>
#include<time.h>

typedef struct NODE {
  struct NODE *next;
  int value;
} Node;

typedef enum { ERROR = 0, OK = 1 } Status;

Status Insert(Node **ppLink,int newValue){//有current版本
    Node *new, *current;

    new = (Node *)malloc(sizeof(Node));
    if(new==NULL){
        return ERROR;
    }
    new->value = newValue;

    while(current=*ppLink,current!=NULL&&current->value<newValue){
        ppLink = &current->next;
    }

    *ppLink = new;
    new->next = current;

    return OK;
}

// Status Insert(Node **ppLink,int newValue){//无*ppLink版本
//     Node *new;

//     new = (Node *)malloc(sizeof(Node));
//     if(new==NULL){
//         return ERROR;
//     }
//     new->value = newValue;

//     while(*ppLink!=NULL&&(*ppLink)->value<newValue){
//         ppLink = &(*ppLink)->next;
//     }

//     new->next = *ppLink;
//     *ppLink = new;

//     return OK;
// }

int main(int argc,char *argv[]){
    Node *linkedList=NULL;
    int temp;
    srand((unsigned)time(NULL));
    for (int i = 0; i < 10;i++){
        temp = rand() % 101;
        printf("%d ", temp);
        Insert(&linkedList,temp);
    }
    printf("\n");
    for (Node *p = linkedList; p!= NULL;p=p->next){
        printf("%d ", p->value);
    }
    return 0;
}

qsort()函数排序法

#include<stdio.h>
#include<stdlib.h>
#include<time.h>


int cmp(const void*a,const void *b){
  return *(int *)a - *(int *)b;
}

int main(int argc,char *argv[]){
  int a[50];
  srand((unsigned)time(NULL));
  for (int i = 0; i < 50;i++){
    a[i] = rand() % 101;
    printf("%d ", a[i]);
  }
  printf("\n");
  qsort(a, 50, sizeof(int), cmp);
  for (int i = 0; i < 50;i++){
    printf("%d ", a[i]);
  }
  return 0;
}

相关文章

  • 各种排序方法

    冒泡排序法 选择排序法 链表排序法 qsort()函数排序法

  • sort 方法各种排序

    1.数组中的元素为Number 2.数组中的元素为String 3.数组中的元素为中文

  • 内排序5:快速排序

    快速排序(划分排序)又被认为是对泡排序的一种改进。在各种排序方法中,这种方法的元素之间的比较次数较少,因而速度较快...

  • JS中的各种排序方法

    1. 介绍 数据结构算法中排序有很多种,常见的、不常见的,至少包含十种以上。根据它们的特性,可以大致分为两种类型...

  • 各种排序

    算法系列,也算是一个总结 + 复习的系列了。因为最近看的《算法(第四版)》就是从排序开始讲起的,那我也有学有样从排...

  • 各种排序

    选择排序 即每次遍历数组选出一个最小的 时间复杂度: n+(n-1)+(n-2)+(n-3)+...+1。约等于O...

  • /2015/06/26/ Bogo 排序

    http://jsdo.it/norahiko/oxIy/fullscreen 这个网页有各种排序方法的直观预览,...

  • 数组排序方法

    数组排序方法介绍 (注意:排序后返回值是不可变数组类型) //排序方法1 (块排序) //排序方法2. //排序...

  • C++编写算法 (四)——排序问题进阶,快速排序

    快速排序,是应用最广泛的排序方法。快速排序流行的原因是它实现简单、适用于各种不同的输入数据且在一般应用中比其他排序...

  • 数据结构实验之排序六:希尔排序

    Problem Description 我们已经学习了各种排序方法,知道在不同的情况下要选择不同的排序算法,以期达...

网友评论

      本文标题:各种排序方法

      本文链接:https://www.haomeiwen.com/subject/rgbxxftx.html