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...

The striped volume offers a maximum amount of space equal to the size of the smallest disk multiplied by the number of disks in the volume that is known as RAID level ________

Fill in the blank(s) with correct word

Computer Science & Information Technology

A(n) notation must supply a name for the data type and provide clues about how applications should handle the data.

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

Computer Science & Information Technology

Qoschin: Your Gateway to Quick, Reliable Answers.