所以我必须创建一个方法,使用quicksort作为排序算法而不使用Java API。然后我必须编写另一个方法,将已排序的数组返回并使用二进制搜索返回true,如果在其中找到搜索到的元素。我不知道我在哪里犯了这个愚蠢的错误。
public class Aufgabe1 {
public static void sort(int[] array) {
/* TODO: add code here */
sort(array, 0, array.length - 1);
}
public static void sort(int[] array, int start, int end) {
int i = start;
int j = end;
int pivot = array[(start+end)/2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (pivot < array[j]) {
j--;
}
if (i <=j) {
int h = array[i];
array[i] = array[j];
array[j] = h;
i++;
j--;
}
}
if (start < i-1) {
sort(array, start, i - 1);
}
if (i < end) {
sort(array, i, end);
}
}
public static boolean binSearch(int[] array, int elem) {
/* TODO: add code here */
int i = 0; //the first element
int j = array.length -1; // the last element
while (i<=j) {
int k = i + ((i+j)/2); //try the middle word
if (elem == array[k]){
return true;
}
if (elem < array[k]) {
j = k-1;
return false;
}else {
i = k+1;
return false;
}
}
return false;
}
//just for testing
public static void main(String[] args) {
int[] arr = new int[] {5, 3, 7, 2, 1, 6};
sort(arr);
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
binSearch(arr,5);
}
答案 0 :(得分:1)
尝试这个,修复了一些小错误,你在错误的地方有几个i和j。虽然,我建议您将代码模块化,因为这样可以使事情更容易阅读并让您更好地了解正在发生的事情。请注意,您实际上并没有在正确的位置返回,因此除非元素位于中间,否则它将始终返回false。另外,不鼓励使用静电。
编辑:另外,我刚注意到你有一个&#39;}&#39;在你应该关闭课程的最后遗漏。 org.springframework.ws.client.WebServiceIOException: I/O error: Connection reset by peer: socket write error; nested exception is java.net.SocketException: Connection reset by peer: socket write error
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:561) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:383) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at ru.fms.sx.adapter.base.sws.SpringWSClientFactory$GenericSWSClient.invokeService(SpringWSClientFactory.java:224) ~[adapters-api-1.0.0-SNAPSHOT.jar:na]
at ru.fms.sx.adapter.base.sws.SpringWSClientFactory$GenericSWSClient.invoke(SpringWSClientFactory.java:163) ~[adapters-api-1.0.0-SNAPSHOT.jar:na]
at $Proxy329.request(Unknown Source) ~[na:na]
at ru.fms.sx.adapter.cregistry.impl.CRegistryRawXmlAdapter.submitImportOrgStructureRequest(CRegistryRawXmlAdapter.java:88) ~[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_05]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_05]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_05]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.7.0_05]
at ru.fms.sx.adapter.container.dto.ExtendedAdapterDto.invokeMethod(ExtendedAdapterDto.java:66) [adapters-container-1.0.0-SNAPSHOT.jar:na]
at ru.fms.sx.kernel.task.impl.TaskProcessor.invokeAdapterMethod(TaskProcessor.java:796) [sx-kernel-1.0.0-SNAPSHOT.jar:na]
at ru.fms.sx.kernel.task.impl.TaskProcessor.processOutgoingTask(TaskProcessor.java:420) [sx-kernel-1.0.0-SNAPSHOT.jar:na]
at ru.fms.sx.kernel.task.impl.DelayedOutgoingTaskExecutor.run(DelayedOutgoingTaskExecutor.java:137) [sx-kernel-1.0.0-SNAPSHOT.jar:na]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.7.0_05]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.7.0_05]
at java.lang.Thread.run(Unknown Source) [na:1.7.0_05]
Caused by: java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.7.0_05]
at java.net.SocketOutputStream.socketWrite(Unknown Source) ~[na:1.7.0_05]
at java.net.SocketOutputStream.write(Unknown Source) ~[na:1.7.0_05]
at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:123) ~[httpcore-4.3.2.jar:4.3.2]
at org.apache.http.impl.io.SessionOutputBufferImpl.write(SessionOutputBufferImpl.java:157) ~[httpcore-4.3.2.jar:4.3.2]
at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:115) ~[httpcore-4.3.2.jar:4.3.2]
at org.apache.http.entity.ByteArrayEntity.writeTo(ByteArrayEntity.java:112) ~[httpcore-4.3.2.jar:4.3.2]
at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:155) ~[httpcore-4.3.2.jar:4.3.2]
at org.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:149) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:236) ~[httpcore-4.3.2.jar:4.3.2]
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121) ~[httpcore-4.3.2.jar:4.3.2]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57) ~[httpclient-4.3.5.jar:4.3.5]
at org.springframework.ws.transport.http.HttpComponentsConnection.onSendAfterWrite(HttpComponentsConnection.java:120) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.ws.transport.AbstractWebServiceConnection.send(AbstractWebServiceConnection.java:48) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.ws.client.core.WebServiceTemplate.sendRequest(WebServiceTemplate.java:654) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:603) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555) ~[spring-ws-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
... 17 common frames omitted