C#数组的排序(正序逆序)

这种排序 超级简单的 !

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {   

            string[] str = { "d","h","a","c","g","t","e","u","b"};

            var lowtohigh = from p in str orderby p select p; //linq
            str = lowtohigh.ToArray();
            foreach (var item in str)
            {
                Console.WriteLine("正序排列" + item);//正序排列
            }
            //=================================================
            for (int i = 0; i < str.Length/2;i ++ )
            {
                string temp=str[i];              //新建一个变量相互交换
                str[i]=str[str.Length-i-1];
                str[str.Length - i - 1] = temp;//相互交换
            }
            foreach (var newstr in str)
            {
                Console.WriteLine("逆序排列:"+newstr);
            }
            Console.WriteLine("===================================");
           //###############################################
            int[] num = { 5, 78, 9, 1, 8, 4, 2, 88, 3 };
            var lowtohigh1 = from p in num orderby p select p;
            num = lowtohigh1.ToArray();
            foreach (var num1 in num)
            {
                Console.WriteLine(num1);//正序排列
            }

            for (int i = 0; i < num.Length / 2;i++ )
            {
               int temp =num[i];             //新建一个变量相互交换
                num[i] = num[num.Length - i - 1];
                num [num.Length - i - 1] = temp;//相互交换
            }
            foreach (var newnum in num)
            {
                Console.WriteLine("逆序排列:" + newnum);
            }
            Console.WriteLine("===================================");

        }
    }
}

  

结果:

C#数组的排序(正序逆序)

时间: 2024-08-29 23:18:01

C#数组的排序(正序逆序)的相关文章

POJ 2299 Ultra-QuickSort (树状数组or 归并排序分治求逆序对数)

题目大意就是说帮你给一些(n个)乱序的数,让你求冒泡排序需要交换数的次数(n<=500000) 显然不能直接模拟冒泡排序,其实交换的次数就是序列的逆序对数. 由于数据范围是 0 ≤ a[i] ≤ 999,999,999所以先要离散化,然后用合适的数据结果求出逆序 可以用线段树一步一步添加a[i],每添加前查询前面添加比它的大的有多少个就可以了. 也可用树状数组,由于树状数组求的是(1...x)的数量和所以每次添加前查询i-sum(a[i])即可 树状数组: //5620K 688MS #incl

Ka递归的编程练习 Part2|做到吐的正序逆序输出、等差数列和

1 #include<stdio.h> 2 void PP(int n) 3 { 4 if(n==0) return; 5 PP(n/10); 6 printf("%d",n%10); 7 } 8 void NP(int n) 9 { 10 if(n==0) return; 11 printf("%d",n%10); 12 PP(n/10); 13 } 14 int Ad(int n) 15 { 16 if(n==0) return 0; 17 if(n

正序逆序生成单链表

typedef struct LNode{ int key; struct LNode *next; }LNode,*List; //正序生成单链表 void CreateList1(List &L,int n){ L=(List)malloc(sizeof(LNode)); L->next=NULL; LNode *q=L; for(int i=1;i<=n;i++) { LNode *p=(LNode *)malloc(sizeof(LNode)); scanf("%d&

UVA 10534-Wavio Sequence(dp_正序逆序最长上升子序列)

Wavio Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem D Wavio Sequence Input: Standard Input Output: Standard Output Time Limit: 2 Seconds Wavio is a sequence of integers. It has some

链表的创建 查找 排序 插入 删除 逆序 长度 显示

#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <stdbool.h> typedef int elementType; typedef struct node {     int data;     struct node1*next; } LinkList; FILE *fp; LinkList*Creat(int length) {     LinkList*hea

java 将一个数组中的值按逆序重新存放,例如,原来顺序为:9,5,7,4,8,要求改为:8,4,7, 5,9。

1 public class Test3 { 2 3 public static void main(String[] args) { 4 5 int[] grade = {87,88,89,98,78}; 6 7 int m; 8 for(int i = 0; i < 2; i++){ 9 10 m = grade[i]; 11 grade[i] = grade[5-i-1]; 12 grade[5-i-1] = m; 13 14 } 15 for(int j =0; j < 5; j++)

输入一列数组,输出它的逆序数组

#include <iostream> using namespace std; int main( ) { int a[10]; for (int i=0; i<10; i++) { cin>>a[i]; } for(int m=9; m>=0; m--) cout<<a[m]<<" "; return 0; } }

c - 逆序/正序输出每位.

1 #include <stdio.h> 2 #include <math.h> 3 4 /* 5 判断一个正整数的位数,并按正序,逆序输出他们的位. 6 */ 7 8 int 9 invert(int); 10 11 void 12 order(int, int); 13 14 int 15 main(void) { 16 int n = 56789; 17 printf("original:%d\n", n); 18 int bitCont = invert

1-6-04:数组逆序重放

描述 将一个数组中的值按逆序重新存放.例如,原来的顺序为8,6,5,4,1.要求改为1,4,5,6,8. 输入 输入为两行:第一行数组中元素的个数n(1<n<100),第二行是n个整数,每两个整数之间用空格分隔. 输出 输出为一行:输出逆序后数组的整数,每两个整数之间用空格分隔. 样例输入 5 8 6 5 4 1 样例输出 1 4 5 6 8 1 #include<stdio.h> 2 int main() 3 { 4 int a[101]={0}; 5 int n; 6 scan