java-多线程知识

几个排序算法比较

  • 采用多线程实现几个排序算法,比较各个排序算法的优劣;
  • java实现,一个主类,多个内部排序算法进程的接口,涉及到进程间的通信,因为每个进程包含自己的储存空间,无法直接访问其他线程的数据,可以在创建线程时,传入数据;
package app;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Thread;

class AppFrame extends JFrame 
{
    //主类数据变量
    int[] Array = new int[20000];   //待排序数组
    Thread[] thread = new Thread[6];//进程数组,用于存储进程


    //构造函数
    public AppFrame() 
    {
        thread[0] = new BubbleSortThread(0);
        thread[1] = new QuickSortThread(1);
        thread[2] = new SelectSortThread(2);
        thread[3] = new InsertSortThread(3);
        thread[4] = new HeapSortThread(4);
        thread[5] = new MergSortThread(5); 
    }

    public void ThreadStart
    {
        for(int i=0;i<thread.length;i++)
        {
            thread[i].start();//启动线程
        }
    }
    //内部类,各类排序算法线程
    class BubbleSortThread extends Thread implements Runnable
    {
        private int key;            //指定类线程在thread数组中的存储位置
        public BubbleSortThread(int k)
        {
            key = k;
        }

        public void run()            //为每个进程重写run功能
        {
            //冒泡算法实现
            int[] bubble= new int[20000];
            System.arraycopy(Array, 0, bubble, 0, 20000);
            boolean exchange = true;
            for (int i = 1; i < bubble.length && exchange; i++) {
                exchange = false;
                for (int j = 0; j < bubble.length - i; j++) {
                    if (bubble[j] > bubble[j + 1]) {
                        int temp = bubble[j];
                        bubble[j] = bubble[j + 1];
                        bubble[j + 1] = temp;
                        exchange = true;
                    }
                }
            }
        }
    }

    //快速排序算法进程实现
    class QuickSortThread extends Thread implements Runnable 
    {
        private int key;
        public QuickSortThread(int k) 
        {
            key = k;
        }
        private void quickSort(int[] table, int low, int hight) 
        {
            if (low < hight) 
            {
                int i = low;
                int j = hight;
                int vot = table[i];
                while (i != j) 
                {
                    while (i < j && vot <= table[j]) 
                    {
                        j--;
                    }
                    if (i < j) 
                    {
                        table[i] = table[j];
                        i++;
                    }
                    while (i < j && table[i] < vot) 
                    {
                        i++;
                    }
                    if (i < j) 
                    {
                        table[j] = table[i];
                        j--;
                    }
                }
                table[i] = vot;
                quickSort(table, low, j - 1);
                quickSort(table, i + 1, hight);
            }
        }
        public void run() 
        {
            int[] table = new int[20000];
            System.arraycopy(Array, 0, table, 0, 20000);
            quickSort(table, 0, table.length - 1);
        }
    }

    //选择排序
    class SelectSortThread extends Thread implements Runnable 
    {
        private int key;
        public SelectSortThread(int k) 
        {
            key = k;
        }

        public void run() 
        {
            int[] table = new int[20000];
            System.arraycopy(Array, 0, table, 0, 20000);

            for (int i = 0; i < table.length - 1; i++) {
                int min = i;
                for (int j = i + 1; j < table.length; j++) {
                    if (table[j] < table[min])
                        min = j;
                    if (min != i) {
                        int temp = table[i];
                        table[i] = table[min];
                        table[min] = temp;
                    }
                }
            }
        }
    }
    
    //插入排序
    class InsertSortThread extends Thread implements Runnable 
    {
        private int key;
        public InsertSortThread(int k) 
        {
            key = k;
        }

        public void run() 
        {
            int[] table = new int[20000];
            System.arraycopy(Array, 0, table, 0, 20000);

            for (int i = 1; i < table.length; i++) {
                int temp = table[i];
                int j;
                for (j = i - 1; j > -1 && temp < table[j]; j--) {
                    table[j + 1] = table[j];
                }
                table[j + 1] = temp;

            }
        }
    }
    
    //小堆排序
    class HeapSortThread extends Thread implements Runnable
    {
        private int key;
        public HeapSortThread(int k) 
        {
            key = k;
        }

        private void sift(int[] table, int low, int hight) 
        {
            int i = low;
            int j = 2 * i + 1;
            int temp = table[i];
            while (j <= hight) 
            {
                if (j < hight && table[j] > table[j + 1])
                    j++;
                if (temp > table[j]) 
                {
                    table[i] = table[j];
                    i = j;
                    j = 2 * i + 1;
                } else
                    j = hight + 1;
            }
            table[i] = temp;
        }

        public void run() 
        {
            int[] table = new int[20000];
            System.arraycopy(Array, 0, table, 0, 20000);
            int n = table.length;

            for (int j = n / 2 - 1; j >= 0; j--) 
            {
                sift(table, j, n - 1);
            }
            for (int j = n - 1; j > 0; j--) 
            {
                int temp = table[0];
                table[0] = table[j];
                table[j] = temp;
                sift(table, 0, j - 1);
            }
        }
    }

    //归并排序
    class MergSortThread extends Thread implements Runnable 
    {
        private int key;
        public MergSortThread(int k) 
        {
            key = k;
        }

        private void merg(int[] x, int[] y, int m, int r, int n) 
        {
            int i = m, j = r, k = m;
            while (i < r && j < r + n && j < x.length) 
            {
                if (x[i] < x[j]) 
                {
                    y[k++] = x[i++];
                }
                else 
                {
                    y[k++] = x[j++];
                }
                while (i < r) 
                {
                    y[k++] = x[i++];
                }
                while (j < r + n && j < x.length) 
                {
                    y[k++] = x[j++];
                }
            }
        }

        private void mergepass(int[] x, int[] y, int n) 
        {
            int i = 0;
            while (i < x.length - 2 * n + 1) 
            {
                merg(x, y, i, i + n, n);
                i += 2 * n;
            }
            if (i + n < x.length) 
            {
                merg(x, y, i, i + n, n);
            } 
            else 
            {
                for (int j = i; j < x.length; j++) 
                {
                    y[j] = x[j];
                }

            }
        }

        public void run() 
        {
            int[] table = new int[20000];
            System.arraycopy(Array, 0, table, 0, 20000);

            int n = 1;
            int[] y = new int[table.length];
            do {
                mergepass(table, y, n);
                n *= 2;
                if (n < table.length) 
                {
                    mergepass(y, table, n);
                    n *= 2;
                }
            } while (n < table.length);
        }
    }
}

public class App {
    public static void main(String[] args) throws Exception {
    AppFrame appframe=new AppFrame();
        appframe.setVisible(true);
        appframe.ThreadStart();//同时启动线程运行
        
    }
}

  • 这指示一个基本框架思路,涉及到一些知识,旨在学习。
发布了89 篇原创文章 · 获赞 8 · 访问量 8885

猜你喜欢

转载自blog.csdn.net/TxyITxs/article/details/103458114