本文介绍了如何调用Java中的冒泡排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个冒泡排序有2组不同的DATAS。目前,我已经进入了冒泡排序方法两次,因此可以与组数据的运行。有没有使用调用方法来冒泡排序的方法。

  INT intJ1R1 =的Integer.parseInt(txtJ1R1.getText());
INT intJ2R1 =的Integer.parseInt(txtJ2R1.getText());
INT intJ3R1 =的Integer.parseInt(txtJ3R1.getText());
INT intJ4R1 =的Integer.parseInt(txtJ4R1.getText());
INT intJ5R1 =的Integer.parseInt(txtJ5R1.getText());
INT intJ6R1 =的Integer.parseInt(txtJ6R1.getText());双[] = r1Array {intJ1R1,intJ2R1,intJ3R1,intJ4R1,intJ5R1,intJ6R1};双温度;
的for(int i = 0; I< r1Array.length;我++)
{
    为(诠释J = 1; J&≤(r1Array.length-i)的; J ++)
    {
        如果(r1Array [J-1]> = r1Array [J]。)
        {
            TEMP = r1Array [J-1];
            r1Array [J-1] = r1Array [J]。
            r1Array [J] =温度;
        }
    }
}双totalR1 =(r1Array [1] + r1Array [2] + r1Array [3] + r1Array [4])/ 4;
字符串stringTotalR1 = Double.toString(totalR1);
lblTotalRun1.setText(stringTotalR1);INT intJ1R2 =的Integer.parseInt(txtJ1R2.getText());
INT intJ2R2 =的Integer.parseInt(txtJ2R2.getText());
INT intJ3R2 =的Integer.parseInt(txtJ3R2.getText());
INT intJ4R2 =的Integer.parseInt(txtJ4R2.getText());
INT intJ5R2 =的Integer.parseInt(txtJ5R2.getText());
INT intJ6R2 =的Integer.parseInt(txtJ6R2.getText());双[] = r2Array {intJ1R2,intJ2R2,intJ3R2,intJ4R2,intJ5R2,intJ6R2};的for(int i = 0; I< r2Array.length;我++)
{
    为(诠释J = 1; J&≤(r2Array.length-i)的; J ++)
    {
        如果(r2Array [J-1]> = r2Array [J]。)
        {
            TEMP = r2Array [J-1];
            r2Array [J-1] = r2Array [J]。
            r2Array [J] =温度;
        }
    }
}双totalR2 =(r2Array [1] + r2Array [2] + r2Array [3] + r2Array [4])/ 4;
字符串stringTotalR2 = Double.toString(totalR2);
lblTotalRun2.setText(stringTotalR2);


解决方案

 公共静态无效的主要(字串[] args)
             {
冒泡BS =新冒泡(); //类的名称,而不是冒泡    双[] = r1Array {intJ1R1,intJ2R1,intJ3R1,intJ4R1,intJ5R1,intJ6R1};
    双[] = r2Array {intJ1R2,intJ2R2,intJ3R2,intJ4R2,intJ5R2,intJ6R2};         双[] = sortedR1 bs.bubbleSort(r1Array);
         双[] = sortedR2 bs.bubbleSort(r2Array);            }
     市民双[]冒泡(双[]数组){
    双温度;         的for(int i = 0; I< array.length,我++)
        {
            为(诠释J = 1; J&≤(array.length-i)的; J ++)
            {
                如果(阵列[J-1]> =阵列[J]。)
                {
                    TEMP =阵列[J-1];
                    阵列[J-1] =阵列[J]。
                    阵列[J] =温度;
                }
            }
        }
         返回数组;
        }

i need to do a bubble sort with 2 different set of datas. currently i have entered the bubble sort method twice so it can run with both set of data. is there a way to use a call method to the bubble sort.

int intJ1R1 = Integer.parseInt(txtJ1R1.getText());
int intJ2R1 = Integer.parseInt(txtJ2R1.getText());
int intJ3R1 = Integer.parseInt(txtJ3R1.getText());
int intJ4R1 = Integer.parseInt(txtJ4R1.getText());
int intJ5R1 = Integer.parseInt(txtJ5R1.getText());
int intJ6R1 = Integer.parseInt(txtJ6R1.getText());

double[] r1Array = {intJ1R1, intJ2R1, intJ3R1, intJ4R1, intJ5R1, intJ6R1};

double temp;
for (int i=0; i<r1Array.length; i++)
{
    for (int j = 1; j <(r1Array.length-i); j++)
    {
        if (r1Array[j-1]>=r1Array[j])
        {
            temp=r1Array[j-1];
            r1Array[j-1] = r1Array[j];
            r1Array[j] = temp;
        }
    }
}

double totalR1 = (r1Array[1] + r1Array[2] + r1Array[3] + r1Array[4])/4;
String stringTotalR1 = Double.toString(totalR1);                
lblTotalRun1.setText(stringTotalR1);

int intJ1R2 = Integer.parseInt(txtJ1R2.getText());
int intJ2R2 = Integer.parseInt(txtJ2R2.getText());
int intJ3R2 = Integer.parseInt(txtJ3R2.getText());
int intJ4R2 = Integer.parseInt(txtJ4R2.getText());
int intJ5R2 = Integer.parseInt(txtJ5R2.getText());
int intJ6R2 = Integer.parseInt(txtJ6R2.getText());

double[] r2Array = {intJ1R2, intJ2R2, intJ3R2, intJ4R2, intJ5R2, intJ6R2};

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

double totalR2 = (r2Array[1] + r2Array[2] + r2Array[3] + r2Array[4])/4;
String stringTotalR2 = Double.toString(totalR2);                
lblTotalRun2.setText(stringTotalR2);
解决方案
         public static void main(String[] args)
             {
BubbleSort bs = new BubbleSort(); //name of your class instead of BubbleSort

    double[] r1Array = {intJ1R1, intJ2R1, intJ3R1, intJ4R1, intJ5R1, intJ6R1};
    double[] r2Array = {intJ1R2, intJ2R2, intJ3R2, intJ4R2, intJ5R2, intJ6R2};

         double[] sortedR1 = bs.bubbleSort(r1Array);   
         double[] sortedR2 = bs.bubbleSort(r2Array); 

            }
     public double[] bubbleSort(double[] array){
    double temp;

         for (int i=0; i<array.length; i++)
        {
            for (int j = 1; j <(array.length-i); j++)
            {
                if (array[j-1]>=array[j])
                {
                    temp=array[j-1];
                    array[j-1] = array[j];
                    array[j] = temp;
                }
            }
        } 
         return array;
        }

这篇关于如何调用Java中的冒泡排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 15:41