Look up how to do quicksort. Write a method to do quicksort of an array of names.

What will be an ideal response?


```
/? ?
? Method t o do a q u i c k s o r t on t h e a r r a y
?/
public void q u i c k S o r t ( )
{
// c h e c k i f t h e a r r a y i s n u l l or has a l e n g t h o f 0 or 1
i f ( a == null | | a . l e n g t h <= 1 )
return ;
else
{
// p i c k a p i v o t i n d e x
int pivotIndex = a . length / 2 ;
// g e t t h e p i v o t v a l u e
String pivotValue = a [ pivotIndex ] ;
// d i s t r i b u t e a l l v a l u e s < t h e p i v o t i n d e x
L i s t l e s s = new A r r a y L i s t ( ) ;
L i s t more = new A r r a y L i s t ( ) ;
f o r ( i nt i = 0 ; i < p i v o t I n d e x ; i ++)
{
i f ( a [ i ] . compareTo ( p i v o t V a l u e ) < 0 )
l e s s . add ( a [ i ] ) ;
else
more . add ( a [ i ] ) ;
}
// d i s t r i b u t e a l l v a l u e s > tha n t h e p i v o t i n d e x
f o r ( i nt i = p i v o t I n d e x + 1 ; i < a . l e n g t h ; i ++)
{
i f ( a [ i ] . compareTo ( p i v o t V a l u e ) < 0 )
l e s s . add ( a [ i ] ) ;
else
more . add ( a [ i ] ) ;
}
// c r e a t e two new S t r i n g A r r a y S o r t e r s and s o r t t h e s e
S t r i n g [ ] l e s s A r r a y = l e s s . toArray (new S t r i n g [ l e s s . s i z e ( ) ] ) ;
S t r i n g A r r a y S o r t e r s o r t e r L e s s = new S t r i n g A r r a y S o r t e r ( l e s s A r r a y ) ;
sorterLess . quickSort ( ) ;
S t r i n g [ ] moreArray = more . toArray (new S t r i n g [ more . s i z e ( ) ] ) ;
S t r i n g A r r a y S o r t e r s o r t e r M o r e = new S t r i n g A r r a y S o r t e r ( moreArray ) ;
sorterMore . quickSort ( ) ;
// p u t t h e s o r t e d e l e m e n t s b a c k i n t h e a r r a y
int i = 0 ;
f o r ( ; i < l e s s A r r a y . l e n g t h ; i ++)
{
a [ i ] = lessArray [ i ] ;
}
a [ i ++] = p i v o t V a l u e ;
f o r ( i nt j = 0 ; j < moreArray . l e n g t h ; j ++)
{
a [ i ++] = moreArray [ j ] ;
}
}
}
```

Computer Science & Information Technology

You might also like to view...

MC The______ layout manager allows the programmer to specify the size and location of components and windows by row and column.

a) Pack. b) Grid. c) Place. d) None of the above.

Computer Science & Information Technology

The taskbar notification area is located on the taskbar

Indicate whether the statement is true or false

Computer Science & Information Technology

Which of the channels defined by the 802.11 standard may be used in the United States?

A. 1 through 11 B. 8 through 24 C. 2 through 32 D. 16 through 32

Computer Science & Information Technology

A table is in first normal form (1NF) if it does not contain repeating groups.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology