Source Code Cross Referenced for CocktailSort.java in  » Testing » TT » U2 » T2 » examples » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » TT » U2.T2.examples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package U2.T2.examples;
002:
003:        import java.util.*;
004:
005:        /** 
006:         * Performs a bi-directional bubble sort on "list", also known as a
007:         * cocktail sort.  After making bubbling from left to right, the
008:         * algorithm then bubbles from right to left. There is a subtle error
009:         * injected that causes the first element is ignored.
010:         *
011:         * <p>Call RT2 on this class with option --elemty=java.lang.Integer
012:         */
013:
014:        public class CocktailSort {
015:            private Comparable[] list;
016:
017:            /**
018:             * The working function for Sort.
019:             */
020:            private void cocktail_sort() {
021:                // INJECTING ERROR, beg should be initialized to 0:
022:                int beg = 1; //Beginning index for the left-to-right pass
023:                int end = list.length - 1; //End index for the left-to-right pass
024:                Comparable tmp = null; //Temporary variable for swapping
025:                boolean hasSwapped = false; //Denotes whether a swap was made during a pass
026:                // main loop:
027:                do {
028:                    hasSwapped = false;
029:
030:                    //Bubble sort from left-to-right starting at beg and ending at end
031:                    for (int i = beg; i < end; ++i) {
032:
033:                        if (list[i].compareTo(list[i + 1]) > 0) {
034:                            tmp = list[i];
035:                            list[i] = list[i + 1];
036:                            list[i + 1] = tmp;
037:                            hasSwapped = true;
038:                        }
039:                    }
040:                    --end; //Decrease end by one
041:
042:                    //Bubble sort from right-to-left starting at end and ending at beg
043:                    for (int i = end; i > beg; --i) {
044:
045:                        if (list[i].compareTo(list[i - 1]) < 0) {
046:                            tmp = list[i];
047:                            list[i] = list[i - 1];
048:                            list[i - 1] = tmp;
049:                            hasSwapped = true;
050:                        }
051:                    }
052:                    ++beg; //Increment beg by one
053:
054:                } while ((hasSwapped == true) && (beg != end)); //While no swaps have been made
055:            }
056:
057:            /**
058:             * Sort the given list.
059:             */
060:            public Comparable[] Sort(Comparable[] unsorted) {
061:
062:                // Initializes list to the passed unsorted list, and then
063:                // invokes the cocktail sort algorithm on that list. The
064:                // resulting sorted list will then be returned.
065:
066:                list = unsorted;
067:                cocktail_sort();
068:                return list;
069:            }
070:
071:            /**
072:             * The specification of Sort.
073:             */
074:            public Comparable[] Sort_spec(Comparable[] unsorted) {
075:
076:                Comparable[] r = Sort(unsorted);
077:                boolean ok = true;
078:                for (int i = 0; i < list.length - 1 && ok; i++)
079:                    ok = list[i].compareTo(list[i + 1]) <= 0;
080:                assert ok : "POST";
081:                return r;
082:            }
083:
084:            /**
085:             * This one will sort a list. The result is put in an array.
086:             */
087:            public Comparable[] Sort(List<Comparable> unsorted) {
088:                Comparable[] array = new Comparable[unsorted.size()];
089:                for (int i = 0; i < unsorted.size(); i++)
090:                    array[i] = unsorted.get(i);
091:                return Sort(array);
092:
093:            }
094:
095:            /**
096:             * The spec. of Sort on List.
097:             */
098:            public Comparable[] Sort_spec(List<Comparable> unsorted) {
099:
100:                Comparable[] r = Sort(unsorted);
101:                boolean ok = true;
102:                for (int i = 0; i < list.length - 1 && ok; i++)
103:                    ok = list[i].compareTo(list[i + 1]) <= 0;
104:                assert ok : "POST";
105:                return r;
106:            }
107:
108:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.