• Shell Sort(草稿)


      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 
      6 namespace ShellSort
      7 {
      8     class DataType
      9     {
     10         public double Data;
     11 
     12         public static bool operator <(DataType a, DataType b)
     13         {
     14             if (a.Data < b.Data)
     15                 return true;
     16 
     17             return false;
     18         }
     19 
     20         public static bool operator >(DataType a, DataType b)
     21         {
     22             if (a.Data > b.Data)
     23                 return true;
     24 
     25             return false;
     26         }
     27 
     28         public DataType()
     29         {
     30             Data = 0;
     31         }
     32     }
     33     class Program
     34     {
     35         public static int count = 0;
     36         static void Main(string[] args)
     37         {
     38             DataType[] array = CreateArray(10);
     39             SetArrayValue(array);
     40             ShellSort(array, 0, array.Length - 1);
     41 
     42             Console.ReadKey();
     43         }
     44 
     45         public static void ShellSort(DataType[] array, int left, int right)
     46         {
     47             DataType temp;
     48             int i, j, gap = right - left + 1;
     49             PrintArray(array);
     50             do
     51             {
     52                 gap = gap / 3 + 1;
     53                 for (i = left + gap; i <= right; i++)
     54                 {
     55                     if (array[i] < array[i - gap])
     56                     {
     57                         temp = array[i];
     58                         j = i - gap;
     59                         do
     60                         {
     61                             array[j + gap] = array[j];
     62                             j = j - gap;
     63                         } while (j > left && temp < array[j]);
     64                         array[j + gap] = temp;
     65                     }
     66                 }
     67                 count++;
     68                 Console.WriteLine("间隔为: {0}", gap);
     69                 PrintArray(array);
     70             } while (gap > 1);
     71         }
     72 
     73         public static void PrintArray(DataType[] array)
     74         {
     75             Console.Write("第{0}次排序结果: ", count);
     76             foreach (var data in array)
     77             {
     78                 if (data != array[array.Length - 1])
     79                 {
     80                     Console.Write(data.Data + " ");
     81                 }
     82                 else
     83                 {
     84                     Console.WriteLine(data.Data);
     85                 }
     86             }
     87         }
     88 
     89         public static void SetArrayValue(DataType[] array)
     90         {
     91             Random random = new Random();
     92             foreach (var data in array)
     93             {
     94                 data.Data = random.Next(0, 20);
     95             }
     96         }
     97 
     98         public static DataType[] CreateArray(int size)
     99         {
    100             if (size < 1)
    101                 return null;
    102             DataType[] temp = new DataType[size];
    103             for (int i = 0; i < size; i++)
    104             {
    105                 temp[i] = new DataType();
    106             }
    107             return temp;
    108         }
    109     }
    110 }
    Code Here
  • 相关阅读:
    pycharm使用技巧
    for 循环
    面向对象进阶
    python实现socket上传下载文件-进度条显示
    python实现进度条--主要用在上传下载文件
    django下常用查询的API
    django下model.py模型的定义
    django下数据库配置
    ORM机制简介
    views.py文件详解
  • 原文地址:https://www.cnblogs.com/AmitX-moten/p/3771572.html
Copyright © 2020-2023  润新知