首页 > 编程语言 >java: Sorting Algorithms

java: Sorting Algorithms

时间:2023-09-28 15:14:55浏览次数:34  
标签:Sorting java int geovindu step Algorithms 2023 array public

 

/**
 * encoding: utf-8
 * 版权所有 2023 ©涂聚文有限公司
 * 许可信息查看: https://www.geeksforgeeks.org/sorting-algorithms/
 * 描述: https://www.geeksforgeeks.org/sorting-algorithms/
 * # Author    : geovindu,Geovin Du 涂聚文. *
 * # IDE       : IntelliJ IDEA 2023.1 Java 21
 * # Datetime  : 2023 - 2023/9/28 - 9:55
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : EssentialAlgorithms
 * # File      : SortingAlgorithm.java
 * # explain   : 学习  Sorting Algorithms 类
 **/

package SortingAlgorithms;

import java.util.Arrays;

public class SortingAlgorithm {



    /**
     *  1。Bubble Sort冒泡排序法
     * @param array 整数数组
     *
     * */
    public static void BubbleSort(int array[]) {

        int size = array.length;

        // loop to access each array element
        for (int i = 0; i < size - 1; i++)

            // loop to compare array elements
            for (int j = 0; j < size - i - 1; j++)

                // compare two adjacent elements
                // change > to < to sort in descending order
                if (array[j] > array[j + 1]) {

                    // swapping occurs if elements
                    // are not in the intended order
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
    }

    /**
     * 2 Selection Sort 选择排序
     * @param array 整数数组
     */
    public static void SelectionSort(int array[]) {
        int size = array.length;

        for (int step = 0; step < size - 1; step++) {
            int min_idx = step;

            for (int i = step + 1; i < size; i++) {

                // To sort in descending order, change > to < in this line.
                // Select the minimum element in each loop.
                if (array[i] < array[min_idx]) {
                    min_idx = i;
                }
            }

            // put min at the correct position
            int temp = array[step];
            array[step] = array[min_idx];
            array[min_idx] = temp;
        }
    }
    /**
     * 3.Insertion Sort 插入排序
     * @param array
     * */
      public static void InsertionSort(int array[]) {
        int size = array.length;

        for (int step = 1; step < size; step++) {
            int key = array[step];
            int j = step - 1;

            // Compare key with each element on the left of it until an element smaller than
            // it is found.
            // For descending order, change key<array[j] to key>array[j].
            while (j >= 0 && key < array[j]) {
                array[j + 1] = array[j];
                --j;
            }

            // Place key at after the element just smaller than it.
            array[j + 1] = key;
        }
    }



}

  

/**
 * encoding: utf-8
 * 版权所有 2023 ©涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * # Author    : geovindu,Geovin Du 涂聚文.
 * # IDE       : IntelliJ IDEA 2023.1 Java 17
 * # Datetime  : 2023 - 2023/9/28 - 10:00
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : EssentialAlgorithms
 * # File      : SortingExmaple.java
 * # explain   : 学习  Sorting Algorithms 类
 **/

package BLL;

import SortingAlgorithms.SortingAlgorithm;
import java.util.Arrays;

public class SortingExmaple {



    /**
     *1.Bubble Sort冒泡排序
     * */
     public static void Bubble()
    {
        int[] geovindu = { -2, 45, 0, 11, -9 };

         // call method using class name
        SortingAlgorithms.SortingAlgorithm.BubbleSort(geovindu);

        System.out.println("1.冒泡排序 Sort Bubble Sorted Array in Ascending Order:");
        System.out.println(Arrays.toString(geovindu));

    }

    /**
     * 2 Selection Sort 选择排序
     */
    public static  void Selection()
    {
        int[] geovindu = { 20, 12, 10, 15, 2 };
        //SelectionSort ss = new SelectionSort();
        SortingAlgorithms.SortingAlgorithm.SelectionSort(geovindu);
        System.out.println("2.选择排序 Selection Sorted Array in Ascending Order: ");
        System.out.println(Arrays.toString(geovindu));

    }

    /**
     * 3. Insertion Sort 插入排序
     * */
    public  static void Insertion()
    {

        int[] geovindu = { 9, 5, 1, 4, 3 };
        SortingAlgorithms.SortingAlgorithm.InsertionSort(geovindu);
        System.out.println("3.插入排序 Insertion Sorted Array in Ascending Order: ");
        System.out.println(Arrays.toString(geovindu));


    }


}

  

调用:

/**
 * encoding: utf-8
 * 版权所有 2023 ©涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * # Author    : geovindu,Geovin Du 涂聚文. *
 * # IDE      : IntelliJ IDEA 2023.1 Java 21
 *
 * # Datetime  : 2023 - 2023/9/28 - 9:55
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : EssentialAlgorithms
 * # File      : Main.java
 * # explain   : 学习 Sorting Algorithms 类
 **/

import BLL.SortingExmaple;


public class Main {

    /**
     *
     * */
    public static void main(String[] args)
    {
        System.out.println("Hello world! Java, 涂聚文 geovindu Geovin Du 学习Java");

        // 1.Bubble Sort冒泡排序法
        SortingExmaple.Bubble();
        //2.
        SortingExmaple.Selection();
        //3.
        SortingExmaple.Insertion();


    }
}

  

 

标签:Sorting,java,int,geovindu,step,Algorithms,2023,array,public
From: https://www.cnblogs.com/geovindu/p/17735813.html

相关文章

  • CSharp: Sorting Algorithms
     /*****************************************************************//***\fileSortingAlgorithm.cs*\briefcsharpSortingAlgorithms算法*IDEvs2022C#.net6.0*\authorgeovindu*\dateSeptember282023**************************......
  • cpp: Sorting Algorithms
     /*****************************************************************//***\fileSortingAlgorithms.h*\brief排序*\IDEvs2022C++20*\authorgeovindu*\dateSeptember282023********************************************************......
  • 安装解压版activemq(版本太高,java不支持)
    1、上传压缩包apache-activemq-5.16.5-bin.tar.gz到/usr/local目录2、解压tar-xzvfapache-activemq-5.16.5-bin.tar.gz3、测试启动,进入/usr/local/apache-activemq-5.16.5/bin目录,启动./activemqstart4、测试访问activemq,访问http://localhost:8161/admin5、修改网页登......
  • 牛客网刷Java记录第一天
    第一题下列程序输出啥?publicclassStringDemo{privatestaticfinalStringMESSAGE="taobao";publicstaticvoidmain(String[]args){Stringa="tao"+"bao";Stringb="tao";Stringc="bao";Sys......
  • 基于Java开发的企事业移动培训考学系统
    一、前言:随着移动技术的飞速发展,企事业培训考试系统也面临着不断的升级和改进。为了更好地满足用户的需求,本文将介绍一款企事业移动培训考学系统,并围绕该系统的功能、特点、应用场景等方面进行详细阐述。二、系统功能企事业移动培训考学系统具有丰富多样的功能,可以满足不同用......
  • JavaScript——小数精度丢失问题
    JavaScript小数进行数值运算时出现精度丢失问题1.原因:JavaScript的number类型在进行运算时都先将十进制转二进制,此时,小数点后面的数字转二进制时会出现无限循环的问题。为了避免这一个情况,要舍0进1,此时就会导致精度丢失问题。2.如何解决:(1)保留小数位数toFixed()constnumObj=......
  • java用Stream一行代码实现数据分组统计、排序、最大值、最小值、平均值、总数、合计
    getAverage():它返回所有接受值的平均值。getCount():它计算所有元素的总数。getMax():它返回最大值。getMin():它返回最小值。getSum():它返回所有元素的总和。示例:@GetMapping("/list")publicvoidlist(){List<InputForm>inputForms=inputFormMapper.se......
  • Java 21 新特性:虚拟线程(Virtual Threads)
    在Java21中,引入了虚拟线程(VirtualThreads)来简化和增强并发性,这使得在Java中编程并发程序更容易、更高效。虚拟线程,也称为“用户模式线程(user-modethreads)”或“纤程(fibers)”。该功能旨在简化并发编程并提供更好的可扩展性。虚拟线程是轻量级的,这意味着它们可以比传统线程创建......
  • java.lang.IllegalStateException: javax.websocket.server.ServerContainer not avai
    spring项目能正常运行,但是单元测试报错错误原因注册WebSocket的Bean与springboot内带tomcat冲突解决办法1.注释该类里面的代码(不推荐)2.@springBootTest注解添加webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT@SpringBootTest注解中,给出了webEnvironment参......
  • Java的类加载顺序
    1.类加载器Java虚拟机的类加载过程是由类加载器(ClassLoader)来实现的。类加载器负责将类装载到内存中,并为其创建一个Class对象。Java虚拟机定义了三种类加载器,分别为BootstrapClassLoader、ExtensionClassLoader、SystemClassLoader,它们按照层次关系进行组织,而且每个类加载器......