飞道的博客

17种编程语言实现排序算法-希尔排序

329人阅读  评论(0)

开源地址

https://gitee.com/lblbc/simple-works/tree/master/sort/

覆盖语言:C、C++、C#、Java、Kotlin、Dart、Go、JavaScript(JS)、TypeScript(TS)、ArkTS、swift、PHP。
覆盖平台:安卓(Java、Kotlin)、iOS(SwiftUI)、Flutter(Dart)、Window桌面(C#)、前端(微信小程序、uni-app、vue)、后端(Java、Kotlin、NodeJS、Python、PHP、Go、C、C++)、鸿蒙


1. 安卓Java版

开发工具:下载Android Studio


    private static void sort(int[] array) {
   
        for (int gap = array.length / 2; gap > 0; gap /= 2) {
   
            for (int i = gap; i < array.length; i++) {
   
                int j = i;
                int tmp = array[j];
                if (array[j] < array[j - gap]) {
   
                    while (j - gap >= 0 && tmp < array[j - gap]) {
   
                        array[j] = array[j - gap];
                        j -= gap;
                    }
                    array[j] = tmp;
                }
            }
        }
    }

 

2. 安卓Kotlin版

开发工具:下载Android Studio


private fun sort(array: IntArray) {
   
    var gap = array.size / 2
    while (gap > 0) {
   
        for (i in gap until array.size) {
   
            var j = i
            val tmp = array[j]
            if (array[j] < array[j - gap]) {
   
                while (j - gap >= 0 && tmp < array[j - gap]) {
   
                    array[j] = array[j - gap]
                    j -= gap
                }
                array[j] = tmp
            }
        }
        gap /= 2
    }
}

 

3. NodeJS

开发工具:下载Visual Studio Code


function sort() {
   
  let array = [2, 1, 5, 4, 3]
  for (let gap = Math.floor(array.length / 2); gap > 0; gap /= 2) {
   
    for (let i = gap; i < array.length; i++) {
   
      var j = i
      var tmp = array[j]
      if (array[j] < array[j - gap]) {
   
        while (j - gap >= 0 && tmp < array[j - gap]) {
   
          array[j] = array[j - gap]
          j -= gap
        }
        array[j] = tmp
      }
    }
  }
}


 

4. Php

开发工具:下载Visual Studio Code

 public function sort()
    {
   
        $arr = [2, 1, 5, 4, 3];
        $length = count($arr);
        $plus = floor($length / 2);
        while ($plus > 0) {
   
            for ($i = 1; $i <= $plus; $i++) {
   
                for ($j = $plus; $j < $length / $plus; $j += $plus) {
   
                    $tmp = $arr[$j];
                    for ($k = $j - $plus; $k >= 0; $k -= $plus) {
   
                        if ($tmp < $arr[$k]) {
   
                            $arr[$k + $plus] = $arr[$k];
                            $arr[$k] = $tmp;
                        }
                    }
                }
            }
            $plus = floor($plus / 2);
        }
        return var_dump($arr);
    }

 

5. Python

开发工具:下载PyCharm


array = [2, 1, 5, 4, 3]


def sort():
    n = len(array)
    gap = int(n / 2)
    while gap > 0:
        for i in range(gap, n):
            temp = array[i]
            j = i
            while j >= gap and array[j - gap] > temp:
                array[j] = array[j - gap]
                j -= gap
            array[j] = temp
        gap = int(gap / 2)

 

6. Swift(SwiftUI版)

开发工具:XCode(mac电脑自带)

var array = [2, 1, 5, 4, 3]

func sort() {
   
    var gap = array.count / 2
    while gap > 0 {
   
        for i in gap ..< array.count {
   
            var formerIndex = i - gap
            while formerIndex >= 0 {
   
                if array[formerIndex] > array[formerIndex+gap] {
   
                    array.swapAt(formerIndex, formerIndex+gap)
                }
                formerIndex -= gap
            }
        }
        gap = gap / 2
    }
}

 

7. uni-app

开发工具:下载HBuilderX

sort() {
   
	let array = [2, 1, 5, 4, 3]
	for (let gap = Math.floor(array.length / 2); gap > 0; gap /= 2) {
   
		for (let i = gap; i < array.length; i++) {
   
			var j = i
			var tmp = array[j]
			if (array[j] < array[j - gap]) {
   
				while (j - gap >= 0 && tmp < array[j - gap]) {
   
					array[j] = array[j - gap]
					j -= gap
				}
				array[j] = tmp
			}
		}
	}
},

 

8. vue

开发工具:下载Visual Studio Code

sort() {
   
      let array = [2, 1, 5, 4, 3]
      for (let gap = Math.floor(array.length / 2); gap > 0; gap /= 2) {
   
        for (let i = gap; i < array.length; i++) {
   
          var j = i
          var tmp = array[j]
          if (array[j] < array[j - gap]) {
   
            while (j - gap >= 0 && tmp < array[j - gap]) {
   
              array[j] = array[j - gap]
              j -= gap
            }
            array[j] = tmp
          }
        }
      }
    }

 

9. 微信小程序

开发工具:下载微信开发者工具

sort() {
   
    let array = [2, 1, 5, 4, 3]
    for (let gap = Math.floor(array.length / 2); gap > 0; gap /= 2) {
   
      for (let i = gap; i < array.length; i++) {
   
        var j = i
        var tmp = array[j]
        if (array[j] < array[j - gap]) {
   
          while (j - gap >= 0 && tmp < array[j - gap]) {
   
            array[j] = array[j - gap]
            j -= gap
          }
          array[j] = tmp
        }
      }
    }
  },

 

10. 鸿蒙(ArkTS)

开发工具:下载DevEco Studio

  sort() {
   
    let array = [2, 1, 5, 4, 3]
    for (let gap = Math.floor(array.length / 2); gap > 0; gap /= 2) {
   
      for (let i = gap; i < array.length; i++) {
   
        var j = i
        var tmp = array[j]
        if (array[j] < array[j - gap]) {
   
          while (j - gap >= 0 && tmp < array[j - gap]) {
   
            array[j] = array[j - gap]
            j -= gap
          }
          array[j] = tmp
        }
      }
    }
  }

 

11. Go语言

开发工具:下载Visual Studio Code


var array = []int{
   2, 1, 5, 4, 3}

func sort() {
   
	for gap := len(array) / 2; gap > 0; gap /= 2 {
   
		for i := gap; i < len(array); i++ {
   
			var j = i
			var tmp = array[j]
			if array[j] < array[j-gap] {
   
				for {
   
					array[j] = array[j-gap]
					j -= gap
					if j-gap < 0 || tmp >= array[j-gap] {
   
						break
					}
				}

				array[j] = tmp
			}
		}
	}
}

 

12. Java

开发工具:下载IntelliJ IDEA


    private static void sort(int[] array) {
   
        for (int gap = array.length / 2; gap > 0; gap /= 2) {
   
            for (int i = gap; i < array.length; i++) {
   
                int j = i;
                int tmp = array[j];
                if (array[j] < array[j - gap]) {
   
                    while (j - gap >= 0 && tmp < array[j - gap]) {
   
                        array[j] = array[j - gap];
                        j -= gap;
                    }
                    array[j] = tmp;
                }
            }
        }
    }

 

13. Kotlin

开发工具:下载IntelliJ IDEA


private fun sort(array: IntArray) {
   
    var gap = array.size / 2
    while (gap > 0) {
   
        for (i in gap until array.size) {
   
            var j = i
            val tmp = array[j]
            if (array[j] < array[j - gap]) {
   
                while (j - gap >= 0 && tmp < array[j - gap]) {
   
                    array[j] = array[j - gap]
                    j -= gap
                }
                array[j] = tmp
            }
        }
        gap /= 2
    }
}

 

14. Flutter

开发工具:下载IntelliJ IDEA

void sort(List<int> array) {
   
  int gap = array.length ~/ 2;
  while (gap > 0) {
   
    for (int i = gap; i < array.length; i++) {
   
      int j = i;
      int tmp = array[j];
      if (array[j] < array[j - gap]) {
   
        while (j - gap >= 0 && tmp < array[j - gap]) {
   
          array[j] = array[j - gap];
          j -= gap;
        }
        array[j] = tmp;
      }
    }
    gap = gap ~/ 2;
  }
}


 

15. C语言

开发工具:下载Visual Studio


void sort(int* array, int count)
{
   
    for (int gap = count / 2; gap > 0; gap /= 2)
    {
   
        for (int i = gap; i < count; i++)
        {
   
            int j = i;
            int tmp = array[j];
            if (array[j] < array[j - gap])
            {
   
                while (j - gap >= 0 && tmp < array[j - gap])
                {
   
                    array[j] = array[j - gap];
                    j -= gap;
                }
                array[j] = tmp;
            }
        }
    }
}


 

16. C++

开发工具:下载Visual Studio


void sort(int* array, int count)
{
   
    for (int gap = count / 2; gap > 0; gap /= 2)
    {
   
        for (int i = gap; i < count; i++)
        {
   
            int j = i;
            int tmp = array[j];
            if (array[j] < array[j - gap])
            {
   
                while (j - gap >= 0 && tmp < array[j - gap])
                {
   
                    array[j] = array[j - gap];
                    j -= gap;
                }
                array[j] = tmp;
            }
        }
    }
}


 

17. C#

开发工具:下载Visual Studio


        private static void Sort(int[] array)
        {
   
            for (int gap = array.Length / 2; gap > 0; gap /= 2)
            {
   
                for (int i = gap; i < array.Length; i++)
                {
   
                    int j = i;
                    int tmp = array[j];
                    if (array[j] < array[j - gap])
                    {
   
                        while (j - gap >= 0 && tmp < array[j - gap])
                        {
   
                            array[j] = array[j - gap];
                            j -= gap;
                        }
                        array[j] = tmp;
                    }
                }
            }
        }

 

关于

厦门大学计算机专业|华为八年高级工程师
专注《零基础学编程系列》 http://lblbc.cn/blog
包含:Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
公众号:蓝不蓝编程


转载:https://blog.csdn.net/yinxing2008/article/details/128748410
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场