Array data type, used in a programming language to specify a variable that can be indexed
During my experiment with array / hash variable, i’ve found this interesting website. The wiki explains how to sort the integers in the array by using a variety of languages. even some programming languages that I do not know at all.
I’ll pick a few examples of sorting functions described in the wiki
Sort an integer array using C language:
#include <stdlib.h> /* qsort() */ #include <stdio.h> /* printf() */ int intcmp(const void *aa, const void *bb) { const int *a = aa; *b = bb; return (*a < *b) ? -1 : (*a > *b); } int main() { int nums[5] = {2,4,3,1,2}; qsort(nums, 5, sizeof(int), intcmp); printf("result: %d %d %d %d %d\n", nums[0], nums[1], nums[2], nums[3], nums[4]); return 0; }
Sort an integer array using C++ language:
#include <algorithm> int main() { int nums[] = {2,4,3,1,2}; std::sort(nums, nums+5); return 0; }
Sort an integer array using C# language:
using System; using System.Collections.Generic; public class Program { static void Main() { int[] unsorted = { 6, 2, 7, 8, 3, 1, 10, 5, 4, 9 }; Array.Sort(unsorted); } }
Sort an integer array using Java language:
import java.util.Arrays; public class example { public static void main(String[] args) { int[] nums = {2,4,3,1,2}; Arrays.sort(nums); } }
Sort an integer array using Perl language:
#!/usr/bin/perl @nums = (2,4,3,1,2); @sorted = sort {$a <=> $b} @nums;
Sort an integer array using php language:
<?php $nums = array(2,4,3,1,2); sort($nums); ?>
Sort an integer array using python language:
nums = [2,4,3,1,2] nums.sort()
Sort an integer array using bash language:
#!/bin/bash function mysort { for i in ${unsorted_array[@]}; do echo "$i"; done | sort -n; } unsorted_array=(1 2 4 3 5) echo "Unsorted array: ${unsorted_array[@]}" sorted_array=( $(mysort) ) echo "Sorted array: ${sorted_array[@]}"
Happy sorting 🙂