遇到QuickSort问题

时间:2018-03-06 05:21:46

标签: sorting quicksort

我有一个数组。

//information.service.ts and authentication.service.ts

import { Injectable, Inject } from '@angular/core';
import { HttpClient} from '@angular/common/http'; //added import
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Injectable()
export class InformationService {

  constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: string) { } //made changes in cunstructor

  add(link: string, title: string, description: string) {
    return this.http.post(this.baseUrl + 'api/Information', { link: link, title: title, description: description })
    .map((response: Response) => {
        console.log(response);
    });
  }
}

Pivot是第一个元素,它是7.因此,左光标是5,右光标是8.在透视和(左右光标)之间进行比较后,两个光标在6处相遇。因此,左光标位于6,右侧光标位于6.我不确定在此步骤后我该怎么办。我应该用枢轴交换6,7还是应该用枢轴交换4?

感谢。

1 个答案:

答案 0 :(得分:0)

您应将枢轴7换为6。 该算法指出,一旦右光标位于与左光标相同的位置或比左光标更远的位置(左光标经过右光标),它将停止迭代。在这种情况下,正如您正确显示的那样,它们都指向6。一旦光标停止迭代,就应该将枢轴与右光标指向的元素交换,在这种情况下为6。

因此,现在7是数组中的分区元素,下一步是递归调用7右边的元素和7左边的元素的quicksort。右边的元素> 7,而左边的所有元素元素<7。继续与上面所述相同的过程,数组将被排序。

相关问题