Начало Новини Маркет Проекти Форум ИТ Работа Уроци Скриптове WiFi точки Още Вход


Рекурсивно сортиране на масив с алгоритъма QuickSort

От gangs7a / 21.04.2010 21:48
Размер на шрифта: A | A | A

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
///BOF
/**
 *
 *
Title: QuickSort Recursive Implementation
 *
Coder: gangs7a
 *
Date : o8.11.2oo8
 *
Using: java QuickSort   
 * 
**/

///
Start Application
public class QuickSort
{
   
public static void main(String[] args)
    {
      
///Initialize Array With 5 Elements
       
int[] array = new int[5];
       
      
///Set Array Elements
        array
[0] = 5;
       
array[1] = 1;
       
array[2] = 3;
       
array[3] = 4;
       
array[4] = 2;
       
       
PrintAbout(); ///Show About Box
        System
.out.println();
       
System.out.println("Not sorted array:");
       
      
///Check For Empty Array
       
if (array.length < 1)
        {
           
System.out.println("Array is empty.");
       
}
       
else
       
{
          
///Print All Array Elements ---> Not Sorted
           
for (int i=0; i<array.length; i++)
            {
               
System.out.println("(" + array[i] + ")");
           
}
        }
       
      
///Sort Array With QuickSort
        QSort
(array, 0, array.length - 1);
       
System.out.println();
       
      
///Check For Empty Array
       
if (array.length < 1)
        {
           
System.out.println("Array is empty.");
       
}
       
else
       
          
///Print All Array Elements ---> Sorted
            System
.out.println("Sorted array:");
           
for (int i=0; i<array.length; i++)
            {
               
System.out.println("(" + array[i] + ")");
           
}
        }
    }
   
  
///QuickSort Algorithm Implementation
    public
static void QSort(int[] array, int start, int end)
    {
       
int i = start;
       
int k = end;
       
       
if (end - start >= 1 )
        {
           
int pivot = array[start];
           
           
while (k > i)
            {
               
while (array[i] <= pivot && i <= end && k > i) i++;
               
while (array[k] > pivot && k >= start && k >= i) k--;
               
               
if (k > i)
                {
                   
Swap(array, i, k);
               
}
            }
           
           
Swap(array, start, k); ///Swap Array Elements
            QSort
(array, start, k - 1); ///Call QuickSort Recursive On
Left Part
            QSort
(array, k + 1, end); ///Call QuickSort Recursive On
Right Part
       
}
       
else
       
{
           
return;
       
}
    }
   
  
///Swap Array Elements Implementation
    public
static void Swap(int[] array, int index1, int index2)
    {
       
int temp = array[index1];
       
array[index1] = array[index2];
       
array[index2] = temp;
   
}
   
  
///Print About Box Implementation
    public
static void PrintAbout()
    {
       
System.out.println("[-------------------------------------]");
       
System.out.println("[--> Title: Recursive QuickSort     --]");
       
System.out.println("[--> Coder: gangs7a                 --]");
       
System.out.println("[--> Date : o8.11.2oo8              --]");
       
System.out.println("[-------------------------------------]");
   
}
}
///
End Application

/*
 *
Example:

D:CodingJava>java QuickSort
[-------------------------------------]
[
--> Title: Recursive QuickSort     --]
[
--> Coder: gangs7a                 --]
[
--> Date : o8.11.2oo8              --]
[
-------------------------------------]

Not sorted array:
(5)
(
1)
(
3)
(
4)
(
2)

Sorted array:
(1)
(
2)
(
3)
(
4)
(
5)

D:CodingJava>

*/
///
EOF

Статистика
Погледнат: пъти
Гласове: --
Оценка:
Действие
 



myTech.bg © 2004 - 2011 | Контакти | За реклама