Quicksorting代码不适用于超过十个字符串

时间:2016-03-13 16:21:40

标签: c

我的代码不超过十个单词,我需要它来排序大约2000个alfabet字符串。和字符串长度小于20个字符。我确保文件的读写是正确的。该文件包含少于20个字符的单词列表,每行包含一个单词。 问题是快速排序功能没有为大量字符串排序。

<template name="adminPublicationsEdit">
    <div class="container extra-spacing-bottom">
    <div class="putdown extra-spacing-bottom">
            <h1 class="extra-spacing-bottom">EDIT ENTRY</h1>
            <hr>
      {{#with pubEntry}}
      <form class="form-horizontal" id="addPub" role="form">
        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">Title</label>
          <div class="col-sm-10">
              <input type="text" class="form-control" id="title" name="title" value="{{title}}" placeholder="Enter Title">
            </div>
        </div>

        <div class="form-group">
            <label for="year" class="col-sm-2 control-label">Year</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="year" name="year" value="{{year}}" placeholder="Enter Year of Publication">
            </div>
        </div>

       <div class="form-group">
          <label for="abstract" class="col-sm-2 control-label">Abstract</label>
          <div class="col-sm-10">
          <div name="abstract" id="abstract">
            {{{abstract}}}
          </div>
          </div>
      </div>

            <div class="form-group js-radios">
                <label for="Outlet" class="col-sm-2 control-label">Outlet</label>
                <div class="col-sm-10">
                    <label class="radio-inline"><input type="radio" name="outlet-type" id="wp" value="wp">Working Paper</label>
                    <label class="radio-inline"><input type="radio" name="outlet-type" id="pp" value="pp">Published Paper</label>
                    <label class="radio-inline"><input type="radio" name="outlet-type" id="bk" value="bk">Book</label>
                    <label class="radio-inline"><input type="radio" name="outlet-type" id="bc" value="bc">Book Chapter</label>
                </div>
            </div>

              {{#if ppSelected}}
               <div class="form-group">
                  <label for="journal" class="col-sm-2 control-label">Journal</label>
                  <div class="col-sm-10">
                      <input type="text" class="form-control" id="journal" name="journal" placeholder="Enter Name of Journal">
                  </div>
              </div>

              <div class="form-group">
                  <label for="pages" class="col-sm-2 control-label">Pages</label>
                  <div class="col-sm-10">
                      <input type="text" class="form-control" id="pages" name="pages" placeholder="Enter Pages">
                  </div>
              </div>
              {{/if}}

            {{#if bkSelected}}
             <div class="form-group">
                <label for="publisher" class="col-sm-2 control-label">Publisher</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="publisher" name="publisher" placeholder="Enter Publisher">
                </div>
            </div>

            <div class="form-group">
                <label for="location" class="col-sm-2 control-label">Location of Publisher</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="location" name="location" placeholder="Enter Location of Publisher">
                </div>
            </div>
            {{/if}}

            {{#if bcSelected}}
             <div class="form-group">
              <label for="publisher" class="col-sm-2 control-label">Publisher</label>
              <div class="col-sm-10">
                  <input type="text" class="form-control" id="publisher" name="publisher" placeholder="Enter Publisher">
              </div>
            </div>

            <div class="form-group">
              <label for="location" class="col-sm-2 control-label">Location of Publisher</label>
              <div class="col-sm-10">
                  <input type="text" class="form-control" id="location" name="location" placeholder="Enter Location of Publisher">
              </div>
            </div>

             <div class="form-group">
                <label for="editors" class="col-sm-2 control-label">Editor(s)</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" name="editors" id="firstEditor"  placeholder="Enter Book Editor(s)">
                </div>
              </div>


            <div class="form-group">
              <label for="pages" class="col-sm-2 control-label">Pages</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="pages" name="pages" value="{{pages}}">
              </div>
            </div>
            {{/if}}

            <div class="form-group">
              <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" id="js-addEntry" class="btn btn-primary admin-button">UPDATE ENTRY</button>
              </div>
            </div>

          </form> <!-- end form -->

      {{/with}}
    </div> <!-- putdown -->
  </div> <!-- container -->
</template>

3 个答案:

答案 0 :(得分:0)

问题在于枢纽。当您在最里面的循环中交换两个元素时:

if(i <= j) {
    strcpy(temp, items[i]);
    strcpy(items[i], items[j]);
    strcpy(items[j], temp);
    i++;
    j--;
}

这两个元素之一items[i]items[j]可能是支点。换句话说,x可能指向其中一个。交换后,x不再指向正确的轴,因为x所指向的元素中的值已更改。

最简单的解决方案是为枢轴分配额外的空间:

...
char *x=items[(left+right)/2];
char pivot[20];
strcpy(pivot, x);
x = pivot;
...

答案 1 :(得分:0)

我还有其他事情可以添加到2501的答案中。

当您检测到无法读取或写入文件时,应退出程序。

我会使用strcpy()而不是memset()来初始化:

for (int n = 0; n < 2000; n++)
{
    strcpy(str[n], "");
}

你很难处理换行符。您应该从阵列中的读取行中删除它们,如果您没有,则应编写警告并跳过行尾,然后在编写文件后在每个单词后添加换行符。我测试了你的程序,有20条长度线或最后一条线,最后没有换行,给出了不好的惊喜。

答案 2 :(得分:-1)

您的快速排序实施错误!!!

这是C-Program中的数组Quicksort

#include <stdio.h>
#include <stdlib.h>

int * quickSort(int *A,int pivotIndex, int N);
int partition(int *A, int pivotIndex, int N);

int main(){
    int N;
    printf("Enter Number of Elements : ");
    scanf("%d",&N);
    int A[N];
    printf("\nEnter Data : ");
    int i=0;
    for(i=0; i<N; i++)
        scanf("%d",&A[i]);

    int *p;
    p = quickSort(A,0,(N-1));

    printf("\nSorted Data : ");
    for(i=0; i<N; i++)
        printf("%d ",p[i]);
    return 0;
}

int * quickSort(int *A, int pivotIndex, int N){

    if(pivotIndex < 0 || pivotIndex > N)
        return ;

    int q = partition(A,pivotIndex, N);
    quickSort(A, 0, (q-1));
    quickSort(A, (q+1), N);

    return A;
}

int partition(int *A, int pivotIndex, int N){

    int i = pivotIndex;
    int flag = 0;

    while(flag == 0){

        flag = 1;
        int j = N;
        int rightFlag = 0, leftFlag = 0;
        while(j > i && rightFlag == 0){

            if(A[i]>A[j]){
                int temp = A[i];
                A[i] = A[j];
                A[j] = temp;
                i = j;
                rightFlag = 1;
                flag = 0;
            }
            j--;
        }
        if(flag == 1)
            break;

        flag = 1;
        j = pivotIndex;
        while(j < i && leftFlag == 0){

            if(A[i]<A[j]){
                int temp = A[i];
                A[i] = A[j];
                A[j] = temp;
                leftFlag = 1;
                flag = 0;
                i = j;
            }
            j++;
        }
    }

    return i;
}